How to detect amount of pixels of some color in area?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

How to detect amount of pixels of some color in area?

Post by VanGog »

Is there a way how to do the following in IM 6.7.5-1 2012-01-28 Q8?

When I have image with some lines like this:

Image

I would like to detect places where the lines are crossed. To be more exact, I am not interested in the places which are crossed...

This is related to my previous thread viewtopic.php?f=1&t=29790&p=133933#p133933 (I asked how to get pixels from some specific area in the image; the area is defined by coordinates). Now, if it is possible to detect the places where is bigger dencity of blue pixels by converting image like this:

Image

You should see that the area where the lines are crossed are slightly more red than the next parts.

Then I would select the place where the colors are acumulated:

Image
(I had to enlarge the image coz the colors were not saved in low resolution)

Then I expand the selection and just delete it

Image

and finally I want to keep the previous task with color selection, (but this time different color). So in the result I will have this:

Image

Which is near location where I originally clicked on a blue line.

To sum it up:

My goel is to get certain part of image based on coordinate, as I asked in previous thread,
and then to remove the places where the pixels are accumulated. Then repeat the previous to get the part of line. My target is to get mask, not the original color.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to detect amount of pixels of some color in area?

Post by snibgo »

To find the crossing, you could skeletonise then find the junction. See http://www.imagemagick.org/Usage/morphology/

Another method is to blur, and find the darkest area.

Code: Select all

convert 2yw64g6.jpg -virtual-pixel White -blur 0x10 -colorspace Gray -auto-level -threshold 5% b.png
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to detect amount of pixels of some color in area?

Post by VanGog »

I have tested your code:
https://1.bp.blogspot.com/-RifbYC9NIUg/ ... /paths.jpg
The problem is that is does not found the junctions except one.

Code: Select all

convert paths.jpg -virtual-pixel White -blur 0x9 -colorspace Gray -auto-level -threshold 5%% junction_width_9.png
convert paths.jpg -virtual-pixel White -blur 0x10 -colorspace Gray -auto-level -threshold 5%% junction_width_10.png
One would expect that if I set 0x10 as the size of brush it should work. If I set 0x6 then if finds but not the correct area (the result is too small).

However, this should work:

Code: Select all

convert lines.gif -morphology HMT LineJunctions hmt_junctions.gif
Image
Image
But it does not work to my image, it does not change it at all (looks so). Nor in grayscale.

Edit:

Code: Select all

I see the problem. What I needed to do:
convert paths.jpg -fuzz 10%% -fill black +opaque "#8697ff" -fuzz 10%% -fill white -opaque "#8697ff"  paths_mask.gif
convert paths_mask.gif -morphology Thinning:-1 Skeleton  paths_thinned.gif
However new problem arises. What if I have this image of route containing gaps:
Image
I need to enlarge the image before I will do thinning. What is better method? Blur it and levels? Or Thicken? The Thicken seems to take too much time and with no result.

blur and threshold is much faster but it is not perfect because some lines were connected. So the bottom routes joined in one fat line

Code: Select all

convert highway_mask.gif -blur 0x4.0 -threshold 25%% highway_threshold_40.png
This is result:
Image

Then when I do the thinning:

Code: Select all

convert highway_threshold_40.png -morphology Thinning:-1 Skeleton  highway_thinned.gif
I found one route bellow instead 1...

Image

Seems like it took 7s to convert the image 1024x728px.
So I tried different command:

Code: Select all

convert highway_threshold_40.png -morphology Thinning:-1 Skeleton  highway_thinned.gif
Image
It seems a bit faster

What if I just make small blur:
Image
how could I get rid of the small black gaps?

Now trying to solve the detection of the rectangles to blur the image with routes. It takes +55 minutes for 1024x768px.

Code: Select all

compare -metric RMSE -subimage-search highway_mask.png small_img_board_1.png result.png
User avatar
bazza
Posts: 20
Joined: 2016-06-15T18:06:36-07:00
Authentication code: 1151
Location: Argntina

Re: How to detect amount of pixels of some color in area?

Post by bazza »

more clean:

Image

Code: Select all

convert  highway_thinned.gif -channel RGB  -morphology Thinning:-1 'LineEnds:1>' result.png
Post Reply