Fabian M
Fabian M

Reputation: 45

Python: Detecting and parametrizing simple rectangular / Gaussian stripe patterns in image matrices

I am looking for a good solution to detect and describe rectangular shaped patterns in a (noisy) matrix image. An example is given below:

Pattern

The image is monochromatic, the colors are just indicating the value of a pixel.

How can I detect these rectangular shapes and save them in a parametrized manner? So far I havent tried anything, but my way forward would be to detect the edges by detecting value differences over a threshold inside certain areas.

Upvotes: 2

Views: 310

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208033

I did this in the command-line with ImageMagick but you can do just the same thing in Python with wand which is based on ImageMagick or with OpenCV using template matching.

Basically, the shape you are looking for is 7x106 pixels. So, I made a black bar exactly that shape and asked ImageMagick to look in a greyscale copy of your image for the best location that most nearly matches such a solid black bar:

Make black bar:

magick -size 7x106 xc:black png8:bar.png

Make your image greyscale:

magick stripes.png -colorspace gray gstripe.png

Search for best black bar in image:

magick compare -dissimilarity-threshold 1 -metric RMSE -subimage-search gstripe.png bar.png r.png
24655.4 (0.376218) @ 33,29

That gives this output:

enter image description here

The 33,29 tells me the top-left of the bar. So, I now draw a white rectangle there to hide the first bar and seek the second:

magick gstripe.png -fill white -draw "rectangle 33,29 40,135" gstripe2.png

magick compare -dissimilarity-threshold 1 -metric RMSE -subimage-search gstripe2.png bar.png r.png
30287.3 (0.462155) @ 162,32

And that tells me the second bar is at coordinates 162,32:

enter image description here


Another method is to use Numpy to calculate the mean of all the columns in your image and then look for the dark areas:

columnMeans = np.mean(im, axis=0)

enter image description here

Upvotes: 2

Related Questions