Skip to main content
6 of 6
deleted 1 character in body
Cris Luengo
  • 7k
  • 1
  • 15
  • 37

np.sum(neighborhood) is computing the mean filter with a rectangular neighborhood (sum filter, really, but since you compare to 0, the scaling doesn’t matter). This filter can be applied in OpenCV with cv.blur.

You then compare to 0 for pixels where image2 is larger than 0. So you can combine the result of the two full-image comparisons with the logical AND operator, and use the result to index into the output image.

I think the following code is equivalent to your loop (not tested, use with care):

final_image[ (cv.blur(image3, [2*K+1, 2*K+1]) > 0) & (image2 > 0) ] = 0 

Note that the complexity of you code is O(n), for n pixels in the image. The double loop iterates once over all pixels. The complexity of the line I wrote here is still O(n). The difference is that the loop over all pixels in OpenCV is written in a compiled language, which is much faster than the Python interpreter.

Note also that the code in the OP computes the sum over the neighborhood only for some pixels, the code in this answer does it for all pixels. But in the OP, each sum is O(K²), whereas here it is O(1), independent of the size of the neighborhood. It is computed with only 3 additions per pixel. So even if we compute the filter at many more pixels, we’re computing it with fewer operations overall (even when ignoring the extra overhead of the Python interpreter).


As I commented on your (now deleted) post over at Stack Overflow, you are using names well established in the field of image processing, and giving them a different meaning. This makes communication difficult. An edge is a sharp transition in intensity, which typically marks the boundary of an object in the image. You referring to a line as an edge is highly confusing.


Now for the code review itself:

image1, image2, image3 are not meaningful names. Other variables holding images have good names. image1 seems to be an alias for original_image, which is not used.

People will complain about variable names K and x, y, because variable names are supposed to be long and descriptive. I personally like x, y for loops over a coordinate system, it is very clear and any other names will be less clear IMO. K could be threshold_distance, for example.

Another typical comment here would be to put your code in a reusable function, scripts are easy to type but much less flexible.

I would also suggest adding more vertical space, a blank line between code blocks can increase readability significantly.

Cris Luengo
  • 7k
  • 1
  • 15
  • 37