|
| 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