Skip to content

Commit ab51fb0

Browse files
fiammantealalek
authored andcommitted
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
1 parent d9f5a62 commit ab51fb0

File tree

4 files changed

+379
-0
lines changed

4 files changed

+379
-0
lines changed

modules/ximgproc/include/opencv2/ximgproc.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "ximgproc/peilin.hpp"
5656
#include "ximgproc/fourier_descriptors.hpp"
5757
#include "ximgproc/ridgefilter.hpp"
58+
#include "ximgproc/brightedges.hpp"
5859

5960

6061
/** @defgroup ximgproc Extended Image Processing
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2017, IBM Corporation, all rights reserved.
14+
// Third party copyrights are property of their respective owners.
15+
//
16+
// @Authors
17+
// Marc Fiammante [email protected]
18+
//
19+
// Redistribution and use in source and binary forms, with or without modification,
20+
// are permitted provided that the following conditions are met:
21+
//
22+
// * Redistribution's of source code must retain the above copyright notice,
23+
// this list of conditions and the following disclaimer.
24+
//
25+
// * Redistribution's in binary form must reproduce the above copyright notice,
26+
// this list of conditions and the following disclaimer in the documentation
27+
// and/or other materials provided with the distribution.
28+
//
29+
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
30+
// derived from this software without specific prior written permission.
31+
//
32+
// This software is provided by the copyright holders and contributors "as is" and
33+
// any express or implied warranties, including, but not limited to, the implied
34+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
35+
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
36+
// indirect, incidental, special, exemplary, or consequential damages
37+
// (including, but not limited to, procurement of substitute goods or services;
38+
// loss of use, data, or profits; or business interruption) however caused
39+
// and on any theory of liability, whether in contract, strict liability,
40+
// or tort (including negligence or otherwise) arising in any way out of
41+
// the use of this software, even if advised of the possibility of such damage.
42+
//
43+
//M*/
44+
#include "opencv2/core.hpp"
45+
namespace cv
46+
{
47+
namespace ximgproc {
48+
CV_EXPORTS_W void BrightEdges(Mat &_original, Mat &_edgeview, int contrast = 1, int shortrange = 3, int longrange = 9);
49+
}
50+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2017, IBM Corporation, all rights reserved.
14+
// Third party copyrights are property of their respective owners.
15+
//
16+
// @Authors
17+
// Marc Fiammante [email protected]
18+
//
19+
// Redistribution and use in source and binary forms, with or without modification,
20+
// are permitted provided that the following conditions are met:
21+
//
22+
// * Redistribution's of source code must retain the above copyright notice,
23+
// this list of conditions and the following disclaimer.
24+
//
25+
// * Redistribution's in binary form must reproduce the above copyright notice,
26+
// this list of conditions and the following disclaimer in the documentation
27+
// and/or other materials provided with the distribution.
28+
//
29+
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
30+
// derived from this software without specific prior written permission.
31+
//
32+
// This software is provided by the copyright holders and contributors "as is" and
33+
// any express or implied warranties, including, but not limited to, the implied
34+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
35+
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
36+
// indirect, incidental, special, exemplary, or consequential damages
37+
// (including, but not limited to, procurement of substitute goods or services;
38+
// loss of use, data, or profits; or business interruption) however caused
39+
// and on any theory of liability, whether in contract, strict liability,
40+
// or tort (including negligence or otherwise) arising in any way out of
41+
// the use of this software, even if advised of the possibility of such damage.
42+
//
43+
//M*/
44+
#include "opencv2/core/utility.hpp"
45+
#include "opencv2/imgproc.hpp"
46+
#include "opencv2/imgcodecs.hpp"
47+
#include "opencv2/highgui.hpp"
48+
#include <stdio.h>
49+
#include <iostream>
50+
#include "opencv2/ximgproc.hpp"
51+
using namespace cv;
52+
using namespace ximgproc;
53+
using namespace std;
54+
55+
static void help()
56+
{
57+
printf("\nThis sample demonstrates BrightEdge detection\n"
58+
"Call:\n"
59+
" /.edge [image_name -- Default is ../data/ml.png]\n\n");
60+
}
61+
const char* keys =
62+
{
63+
"{help h||}{@image |../data/ml.png|input image name}"
64+
};
65+
int main(int argc, const char** argv)
66+
{
67+
CommandLineParser parser(argc, argv, keys);
68+
if (parser.has("help"))
69+
{
70+
help();
71+
return 0;
72+
}
73+
string filename = parser.get<string>(0);
74+
Mat image = imread(filename, IMREAD_COLOR);
75+
if (image.empty())
76+
{
77+
printf("Cannot read image file: %s\n", filename.c_str());
78+
help();
79+
return -1;
80+
}
81+
// Create a window
82+
// // " original ";
83+
namedWindow("Original");
84+
imshow("Original", image);
85+
// " absdiff ";
86+
Mat edge;
87+
BrightEdges(image, edge, 0); // No contrast
88+
namedWindow("Absolute Difference");
89+
imshow("Absolute Difference", edge);
90+
// " default contrast 1 ";
91+
BrightEdges(image, edge);
92+
namedWindow("Default contrast");
93+
imshow("Default contrast", edge);// Default contrast 1
94+
// " Contrast 5 \n";
95+
BrightEdges(image, edge, 5);
96+
namedWindow("Contrast 5");
97+
imshow("Contrast 5", edge);
98+
// " Contrast 10 \n";
99+
BrightEdges(image, edge, 10);
100+
namedWindow("Contrast 10");
101+
imshow("Contrast 10", edge);
102+
// "wait key ";
103+
waitKey(0);
104+
// "end ";
105+
return 0;
106+
}

modules/ximgproc/src/brightedges.cpp

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2017, IBM Corporation, all rights reserved.
14+
// Third party copyrights are property of their respective owners.
15+
//
16+
// @Authors
17+
// Marc Fiammante [email protected]
18+
//
19+
// Redistribution and use in source and binary forms, with or without modification,
20+
// are permitted provided that the following conditions are met:
21+
//
22+
// * Redistribution's of source code must retain the above copyright notice,
23+
// this list of conditions and the following disclaimer.
24+
//
25+
// * Redistribution's in binary form must reproduce the above copyright notice,
26+
// this list of conditions and the following disclaimer in the documentation
27+
// and/or other materials provided with the distribution.
28+
//
29+
// * The name of OpenCV Foundation or contributors may not be used to endorse or promote products
30+
// derived from this software without specific prior written permission.
31+
//
32+
// This software is provided by the copyright holders and contributors "as is" and
33+
// any express or implied warranties, including, but not limited to, the implied
34+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
35+
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
36+
// indirect, incidental, special, exemplary, or consequential damages
37+
// (including, but not limited to, procurement of substitute goods or services;
38+
// loss of use, data, or profits; or business interruption) however caused
39+
// and on any theory of liability, whether in contract, strict liability,
40+
// or tort (including negligence or otherwise) arising in any way out of
41+
// the use of this software, even if advised of the possibility of such damage.
42+
//
43+
//M*/
44+
#include "opencv2/ximgproc.hpp"
45+
#include "precomp.hpp"
46+
#include <iostream>
47+
#include <signal.h>
48+
namespace cv
49+
{
50+
namespace ximgproc {
51+
static bool isPixelMinimum(Mat &edge, int row, int col, int contrast) {
52+
int count = 0;
53+
int pixel = edge.ptr(row)[col] + contrast - 1; // minus 1 is needed for chessboard like images with contrast = 1
54+
// to get the vertical borders
55+
int m2 = edge.ptr(row - 2)[col - 2];
56+
int m1 = edge.ptr(row - 1)[col - 1];
57+
int p1 = edge.ptr(row + 1)[col + 1];
58+
int p2 = edge.ptr(row + 2)[col + 2];
59+
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum diagonal
60+
m2 = edge.ptr(row - 2)[col];
61+
m1 = edge.ptr(row - 1)[col];
62+
p1 = edge.ptr(row + 1)[col];
63+
p2 = edge.ptr(row + 2)[col];
64+
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum vertical
65+
m2 = edge.ptr(row - 2)[col + 2];
66+
m1 = edge.ptr(row - 1)[col + 1];
67+
p1 = edge.ptr(row + 1)[col - 1];
68+
p2 = edge.ptr(row + 2)[col - 2];
69+
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum other diagonal
70+
m2 = edge.ptr(row)[col + 2];
71+
m1 = edge.ptr(row)[col + 1];
72+
p1 = edge.ptr(row)[col - 1];
73+
p2 = edge.ptr(row)[col - 2];
74+
if ((pixel <= m1) && (pixel <= p1) && (pixel < (m1 + m2) / 2) && (pixel < (p1 + p2) / 2)) count++; // Local minimum horizontal
75+
if (count > 1) return true; // Avoid corners of black zones
76+
return false;
77+
}
78+
static int correctPixel(Mat &iedge, int row, int col) {
79+
// now check in there is a line around pixel to fill gaps
80+
// Around Diagonal top left to bottom right
81+
int weight = 4 * 255;
82+
int lines = 0;
83+
int line =
84+
iedge.ptr(row - 1)[col - 2] +
85+
iedge.ptr(row - 1)[col - 1] +
86+
iedge.ptr(row)[col + 1] +
87+
iedge.ptr(row)[col + 2];
88+
if (line == 0) lines += 1;
89+
// Around horizontal
90+
line =
91+
iedge.ptr(row)[col - 2] +
92+
iedge.ptr(row)[col - 1] +
93+
iedge.ptr(row + 1)[col + 1] +
94+
iedge.ptr(row + 1)[col + 2];
95+
if (line == 0) lines += 1;
96+
// Around Diagonal top right to bottom left
97+
line =
98+
iedge.ptr(row - 2)[col] +
99+
iedge.ptr(row - 1)[col] +
100+
iedge.ptr(row + 1)[col + 1] +
101+
iedge.ptr(row + 2)[col + 1];
102+
if (line == 0) lines += 1;
103+
// Around vertical
104+
line =
105+
iedge.ptr(row - 2)[col - 1] +
106+
iedge.ptr(row - 1)[col - 1] +
107+
iedge.ptr(row + 1)[col] +
108+
iedge.ptr(row + 2)[col];
109+
if (line == 0) lines += 1;
110+
line =
111+
iedge.ptr(row - 2)[col - 2] +
112+
iedge.ptr(row - 1)[col - 2] +
113+
iedge.ptr(row - 2)[col - 1] +
114+
iedge.ptr(row - 1)[col - 1] +
115+
iedge.ptr(row + 1)[col + 1] +
116+
iedge.ptr(row + 1)[col + 2] +
117+
iedge.ptr(row + 2)[col + 1] +
118+
iedge.ptr(row + 2)[col + 2];
119+
if (line < weight) lines += 1;
120+
// Near vertical
121+
line =
122+
iedge.ptr(row - 2)[col] +
123+
iedge.ptr(row - 1)[col] +
124+
iedge.ptr(row - 2)[col - 1] +
125+
iedge.ptr(row - 2)[col + 1] +
126+
iedge.ptr(row + 1)[col] +
127+
iedge.ptr(row + 2)[col] +
128+
iedge.ptr(row + 2)[col + 1] +
129+
iedge.ptr(row + 2)[col - 1];
130+
if (line < weight) lines += 1;
131+
// Near diagonal top right to bottom left
132+
line =
133+
iedge.ptr(row - 2)[col + 2] +
134+
iedge.ptr(row - 1)[col + 1] +
135+
iedge.ptr(row - 2)[col - 1] +
136+
iedge.ptr(row - 1)[col + 2] +
137+
iedge.ptr(row + 1)[col - 1] +
138+
iedge.ptr(row + 2)[col - 2] +
139+
iedge.ptr(row + 2)[col - 1] +
140+
iedge.ptr(row + 1)[col - 2];
141+
if (line < weight) lines += 1;
142+
// Near horizontal
143+
line =
144+
iedge.ptr(row)[(col - 2)] +
145+
iedge.ptr(row)[(col - 1)] +
146+
iedge.ptr(row - 1)[(col - 2)] +
147+
iedge.ptr(row + 1)[(col - 2)] +
148+
iedge.ptr(row)[col + 1] +
149+
iedge.ptr(row)[col + 2] +
150+
iedge.ptr(row + 1)[col + 2] +
151+
iedge.ptr(row - 1)[col + 2];
152+
if (line < weight) lines += 1;
153+
if (line == 1) return 0;
154+
// Compute surrounding pixels for dark zone
155+
int surround = iedge.ptr(row - 1)[col - 1] +
156+
iedge.ptr(row - 1)[col] +
157+
iedge.ptr(row - 1)[col + 1] +
158+
iedge.ptr(row)[col - 1] +
159+
iedge.ptr(row)[col + 1] +
160+
iedge.ptr(row + 1)[col - 1] +
161+
iedge.ptr(row + 1)[col] +
162+
iedge.ptr(row + 1)[col + 1];
163+
if (surround == 8 * 255) return 255;
164+
if (surround == 0) return 255;
165+
return iedge.ptr(row)[col];
166+
}
167+
static int contrastEdges(Mat &minput, Mat &mouput, int contrast) {
168+
Mat mwork(minput.size(), minput.type(), Scalar(255));
169+
// Now find if other pixels inside are minimum
170+
for (int row = 2; row < minput.rows - 2; row++) {
171+
for (int col = 2; col < minput.cols - 2; col++) {
172+
if (isPixelMinimum(minput, row, col, contrast)) {
173+
mwork.ptr(row)[col] = 0;
174+
}
175+
else {
176+
mwork.ptr(row)[col] = 255;
177+
}
178+
}
179+
}
180+
// correct pixels
181+
for (int row = 2; row < mwork.rows - 2; row++) {
182+
for (int col = 2; col < mwork.cols - 2; col++) {
183+
mouput.ptr(row)[col] = (uchar)correctPixel(mwork, row, col);
184+
}
185+
}
186+
// Set border of output matrix to white
187+
for (int col = 0; col < mouput.cols; col++) {
188+
for (int row = 0; row < 2; row++) {
189+
mouput.ptr(row)[col] = 255;
190+
}
191+
for (int row = mouput.rows - 2; row < mouput.rows; row++) {
192+
mouput.ptr(row)[col] = 255;
193+
}
194+
}
195+
for (int row = 0; row < mouput.rows; row++) {
196+
for (int col = 0; col < 2; col++) {
197+
mouput.ptr(row)[col] = 255;
198+
}
199+
for (int col = mouput.cols - 2; col < mouput.cols; col++) {
200+
mouput.ptr(row)[col] = 255;
201+
}
202+
}
203+
return 0;
204+
}
205+
CV_EXPORTS_W void BrightEdges(Mat &image, Mat &edge, int contrast, int shortrange, int longrange)
206+
{
207+
Mat gray, gblur, bblur, diff, cedge;
208+
GaussianBlur(image, gblur, Size(shortrange, shortrange), 0);
209+
blur(image, bblur, Size(longrange, longrange));
210+
absdiff(gblur, bblur, diff);
211+
cvtColor(diff, gray, COLOR_BGR2GRAY);
212+
equalizeHist(gray, cedge);
213+
if (contrast > 0) {
214+
edge = Mat(cedge.size(), cedge.type());
215+
contrastEdges(cedge, edge, contrast);
216+
}
217+
else {
218+
edge = cedge;
219+
}
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)