Commit ab51fb06 authored by fiammante's avatar fiammante Committed by Alexander Alekhin

Merge pull request #1449 from fiammante:bright_edges

Bright edges source code and demo (#1449)

* Bright edges source code and demo

* Fix mac errors

* Upload again because of null characters

* Convert to UTF-8

* Change cv::WindowFlags::WINDOW_AUTOSIZE

* Remove parameter

* Remove trailing white spaces (documentation builld error

* Remaining white spaces

* Correct warning

* Correct surround

* Add comment

* Moving prototype out of header

* Add ximgproc namespace, fix example.

* Fix namespace for example

* Remove windows desctruction

* Correct Mat declaration

* Removed unused gray

* Change local functions to static

* Remove tabs and replace by 4 spaces

* Add comments

* Fix comments

* Retry for random test stereo failure
parent d9f5a629
......@@ -55,6 +55,7 @@
#include "ximgproc/peilin.hpp"
#include "ximgproc/fourier_descriptors.hpp"
#include "ximgproc/ridgefilter.hpp"
#include "ximgproc/brightedges.hpp"
/** @defgroup ximgproc Extended Image Processing
......
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2017, IBM Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Marc Fiammante marc.fiammante@fr.ibm.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "opencv2/core.hpp"
namespace cv
{
namespace ximgproc {
CV_EXPORTS_W void BrightEdges(Mat &_original, Mat &_edgeview, int contrast = 1, int shortrange = 3, int longrange = 9);
}
}
\ No newline at end of file
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2017, IBM Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Marc Fiammante marc.fiammante@fr.ibm.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <stdio.h>
#include <iostream>
#include "opencv2/ximgproc.hpp"
using namespace cv;
using namespace ximgproc;
using namespace std;
static void help()
{
printf("\nThis sample demonstrates BrightEdge detection\n"
"Call:\n"
" /.edge [image_name -- Default is ../data/ml.png]\n\n");
}
const char* keys =
{
"{help h||}{@image |../data/ml.png|input image name}"
};
int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv, keys);
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>(0);
Mat image = imread(filename, IMREAD_COLOR);
if (image.empty())
{
printf("Cannot read image file: %s\n", filename.c_str());
help();
return -1;
}
// Create a window
// // " original ";
namedWindow("Original");
imshow("Original", image);
// " absdiff ";
Mat edge;
BrightEdges(image, edge, 0); // No contrast
namedWindow("Absolute Difference");
imshow("Absolute Difference", edge);
// " default contrast 1 ";
BrightEdges(image, edge);
namedWindow("Default contrast");
imshow("Default contrast", edge);// Default contrast 1
// " Contrast 5 \n";
BrightEdges(image, edge, 5);
namedWindow("Contrast 5");
imshow("Contrast 5", edge);
// " Contrast 10 \n";
BrightEdges(image, edge, 10);
namedWindow("Contrast 10");
imshow("Contrast 10", edge);
// "wait key ";
waitKey(0);
// "end ";
return 0;
}
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2017, IBM Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Marc Fiammante marc.fiammante@fr.ibm.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "opencv2/ximgproc.hpp"
#include "precomp.hpp"
#include <iostream>
#include <signal.h>
namespace cv
{
namespace ximgproc {
static bool isPixelMinimum(Mat &edge, int row, int col, int contrast) {
int count = 0;
int pixel = edge.ptr(row)[col] + contrast - 1; // minus 1 is needed for chessboard like images with contrast = 1
// to get the vertical borders
int m2 = edge.ptr(row - 2)[col - 2];
int m1 = edge.ptr(row - 1)[col - 1];
int p1 = edge.ptr(row + 1)[col + 1];
int p2 = edge.ptr(row + 2)[col + 2];
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum diagonal
m2 = edge.ptr(row - 2)[col];
m1 = edge.ptr(row - 1)[col];
p1 = edge.ptr(row + 1)[col];
p2 = edge.ptr(row + 2)[col];
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum vertical
m2 = edge.ptr(row - 2)[col + 2];
m1 = edge.ptr(row - 1)[col + 1];
p1 = edge.ptr(row + 1)[col - 1];
p2 = edge.ptr(row + 2)[col - 2];
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum other diagonal
m2 = edge.ptr(row)[col + 2];
m1 = edge.ptr(row)[col + 1];
p1 = edge.ptr(row)[col - 1];
p2 = edge.ptr(row)[col - 2];
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum horizontal
if (count > 1) return true; // Avoid corners of black zones
return false;
}
static int correctPixel(Mat &iedge, int row, int col) {
// now check in there is a line around pixel to fill gaps
// Around Diagonal top left to bottom right
int weight = 4 * 255;
int lines = 0;
int line =
iedge.ptr(row - 1)[col - 2] +
iedge.ptr(row - 1)[col - 1] +
iedge.ptr(row)[col + 1] +
iedge.ptr(row)[col + 2];
if (line == 0) lines += 1;
// Around horizontal
line =
iedge.ptr(row)[col - 2] +
iedge.ptr(row)[col - 1] +
iedge.ptr(row + 1)[col + 1] +
iedge.ptr(row + 1)[col + 2];
if (line == 0) lines += 1;
// Around Diagonal top right to bottom left
line =
iedge.ptr(row - 2)[col] +
iedge.ptr(row - 1)[col] +
iedge.ptr(row + 1)[col + 1] +
iedge.ptr(row + 2)[col + 1];
if (line == 0) lines += 1;
// Around vertical
line =
iedge.ptr(row - 2)[col - 1] +
iedge.ptr(row - 1)[col - 1] +
iedge.ptr(row + 1)[col] +
iedge.ptr(row + 2)[col];
if (line == 0) lines += 1;
line =
iedge.ptr(row - 2)[col - 2] +
iedge.ptr(row - 1)[col - 2] +
iedge.ptr(row - 2)[col - 1] +
iedge.ptr(row - 1)[col - 1] +
iedge.ptr(row + 1)[col + 1] +
iedge.ptr(row + 1)[col + 2] +
iedge.ptr(row + 2)[col + 1] +
iedge.ptr(row + 2)[col + 2];
if (line < weight) lines += 1;
// Near vertical
line =
iedge.ptr(row - 2)[col] +
iedge.ptr(row - 1)[col] +
iedge.ptr(row - 2)[col - 1] +
iedge.ptr(row - 2)[col + 1] +
iedge.ptr(row + 1)[col] +
iedge.ptr(row + 2)[col] +
iedge.ptr(row + 2)[col + 1] +
iedge.ptr(row + 2)[col - 1];
if (line < weight) lines += 1;
// Near diagonal top right to bottom left
line =
iedge.ptr(row - 2)[col + 2] +
iedge.ptr(row - 1)[col + 1] +
iedge.ptr(row - 2)[col - 1] +
iedge.ptr(row - 1)[col + 2] +
iedge.ptr(row + 1)[col - 1] +
iedge.ptr(row + 2)[col - 2] +
iedge.ptr(row + 2)[col - 1] +
iedge.ptr(row + 1)[col - 2];
if (line < weight) lines += 1;
// Near horizontal
line =
iedge.ptr(row)[(col - 2)] +
iedge.ptr(row)[(col - 1)] +
iedge.ptr(row - 1)[(col - 2)] +
iedge.ptr(row + 1)[(col - 2)] +
iedge.ptr(row)[col + 1] +
iedge.ptr(row)[col + 2] +
iedge.ptr(row + 1)[col + 2] +
iedge.ptr(row - 1)[col + 2];
if (line < weight) lines += 1;
if (line == 1) return 0;
// Compute surrounding pixels for dark zone
int surround = iedge.ptr(row - 1)[col - 1] +
iedge.ptr(row - 1)[col] +
iedge.ptr(row - 1)[col + 1] +
iedge.ptr(row)[col - 1] +
iedge.ptr(row)[col + 1] +
iedge.ptr(row + 1)[col - 1] +
iedge.ptr(row + 1)[col] +
iedge.ptr(row + 1)[col + 1];
if (surround == 8 * 255) return 255;
if (surround == 0) return 255;
return iedge.ptr(row)[col];
}
static int contrastEdges(Mat &minput, Mat &mouput, int contrast) {
Mat mwork(minput.size(), minput.type(), Scalar(255));
// Now find if other pixels inside are minimum
for (int row = 2; row < minput.rows - 2; row++) {
for (int col = 2; col < minput.cols - 2; col++) {
if (isPixelMinimum(minput, row, col, contrast)) {
mwork.ptr(row)[col] = 0;
}
else {
mwork.ptr(row)[col] = 255;
}
}
}
// correct pixels
for (int row = 2; row < mwork.rows - 2; row++) {
for (int col = 2; col < mwork.cols - 2; col++) {
mouput.ptr(row)[col] = (uchar)correctPixel(mwork, row, col);
}
}
// Set border of output matrix to white
for (int col = 0; col < mouput.cols; col++) {
for (int row = 0; row < 2; row++) {
mouput.ptr(row)[col] = 255;
}
for (int row = mouput.rows - 2; row < mouput.rows; row++) {
mouput.ptr(row)[col] = 255;
}
}
for (int row = 0; row < mouput.rows; row++) {
for (int col = 0; col < 2; col++) {
mouput.ptr(row)[col] = 255;
}
for (int col = mouput.cols - 2; col < mouput.cols; col++) {
mouput.ptr(row)[col] = 255;
}
}
return 0;
}
CV_EXPORTS_W void BrightEdges(Mat &image, Mat &edge, int contrast, int shortrange, int longrange)
{
Mat gray, gblur, bblur, diff, cedge;
GaussianBlur(image, gblur, Size(shortrange, shortrange), 0);
blur(image, bblur, Size(longrange, longrange));
absdiff(gblur, bblur, diff);
cvtColor(diff, gray, COLOR_BGR2GRAY);
equalizeHist(gray, cedge);
if (contrast > 0) {
edge = Mat(cedge.size(), cedge.type());
contrastEdges(cedge, edge, contrast);
}
else {
edge = cedge;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment