Get coordinates Top, Left, size of some areas

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?".
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Get coordinates Top, Left, size of some areas

Post by cgkas »

Hello. I have this input image, for which I want to get the coordinates Top, Left, W, H of the resulting white rectangles and Top, Left, W, H of resulting black rectangles that are surrouned by 1 pixel white line.

input.png
Image

After doing this I get the areas I want in blank and white:

Code: Select all

convert input.png \
-transparent '#39D6AE' \
-transparent '#9EB629' \
-transparent '#8DA718' \
-transparent '#A47721' \
-transparent '#BD8B2B' \
-transparent '#EEEEEE' \
-transparent '#B4CEF8' \
-alpha extract -negate \
black_and_white.png
output black and white
Image
I think I'm close to get my goal. So, may you give a hand please. With connected-components maybe?

Thanks for any help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

In Unix bash syntax starting with your black and white image:'

Code: Select all

convert Black_and_White.jpeg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-define connected-components:mean-color=true \
-connected-components 4 \
+write tmp.png null: | grep "gray(255)" | sed 's/^[ ]*//' | cut -d\  -f2

Code: Select all

423x145+530+203
423x145+70+203
264x40+680+109
Where tmp.png is:
Image


see https://imagemagick.org/script/connected-components.php
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Get coordinates Top, Left, size of some areas

Post by cgkas »

Thanks fmw42. It seems to work pretty fine for white reactangles!

Now how can I get the same coordinates and size for the rectangles that are surrounded by the one pixel white line?

Those 4 rectangles in black are below.

Left, Top, Width X Height
71, 425, 420x458
531, 425, 421x158
71, 635, 420x308
531, 635, 421x308
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

Best if you provide an example image so I understand the issue.
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Get coordinates Top, Left, size of some areas

Post by cgkas »

You got the coordinates of white rectangles fine.

I still would like to get the coordinates and size of black rectangles surrounded by the white line.

In the image below I show those rectangles I want in red.

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

I am not sure how to do that unless you can color them first so they are filled in. If the lines were separate and did not connect between the box areas AND there were no extended lines, then then could be identified. But the connected components will get the bounding boxes including the extended lines. Perhaps some morphology might be able to remove the extension, but I am not sure how to do that. Actually one pass of connected components can identify the white border regions on the black boxes with some filtering and you can then fill them with white using the centroid coordinates and -draw "color $centroid floodfill". Once they are filled with white, some morphology can remove those extensions. Then another pass of connected components can locates the bounding boxes for all the white box regions again.

PS Your input.png has been change to JPG by your image hosting service. That causes JPG compression which changes your colors so that your command does not work. Please repost to some place such as dropbox.com or zip your input file first so that it does not get changed from PNG,
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get coordinates Top, Left, size of some areas

Post by snibgo »

Perhaps I misunderstand the question. I think cgkas simply wants the coordinates of the black connected components:

Code: Select all

magick Black_and_White.png -threshold 50% +write x.png -define connected-components:verbose=true -define connected-components:area-threshold=10 -connected-components 4 NULL:
The first few lines are:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 990x949+0+0 465.3,331.3 408770 srgb(0,0,0)
  141: 421x308+531+635 741.0,788.5 129668 srgb(0,0,0)
  140: 420x308+71+635 280.5,788.5 129360 srgb(0,0,0)
  135: 421x158+531+425 741.0,503.5 66518 srgb(0,0,0)
  134: 420x158+71+425 280.5,503.5 66360 srgb(0,0,0)
  28: 423x145+530+203 741.2,275.4 55780 srgb(255,255,255)
  27: 423x145+70+203 281.1,275.4 55703 srgb(255,255,255)
  1: 264x40+680+109 812.1,128.2 9343 srgb(255,255,255)
  139: 423x355+530+589 741.0,778.2 1552 srgb(255,255,255)
  138: 422x355+70+589 280.5,778.2 1550 srgb(255,255,255)
  133: 423x205+530+379 741.0,496.1 1252 srgb(255,255,255)
  132: 422x205+70+379 280.5,496.1 1250 srgb(255,255,255)
  35: 46x32+725+222 747.0,236.1 946 srgb(0,0,0)
  31: 46x32+266+219 288.0,233.1 946 srgb(0,0,0)
  36: 28x32+776+222 789.1,236.9 657 srgb(0,0,0)
The largest black box ("srgb(0,0,0)") is the entire image. The next four are the ones required.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

snibgo wrote:Perhaps I misunderstand the question. I think cgkas simply wants the coordinates of the black connected components:
Yes, I agree with you. Good point. There is no need to fill then with white, since they are surrounded by white. Thus you can find the black rectangles as well as the white one apart from the background black.
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Get coordinates Top, Left, size of some areas

Post by cgkas »

Excellent snibgo,

I see your code it works pretty fine taking the Black and White image as input.

So, I think there is no strigth forward way, is need to make black and white first and then apply your code that prints the white reactangles too.


Is there a way to mix in a single command the 2 steps below to make it more straight forward?

STEP1 (Convert to black and white)

Code: Select all

convert input.png \
-transparent '#39D6AE' \
-transparent '#9EB629' \
-transparent '#8DA718' \
-transparent '#A47721' \
-transparent '#BD8B2B' \
-transparent '#EEEEEE' \
-transparent '#B4CEF8' \
-alpha extract -negate \
Black_and_White.png
STEP2 (Get coordinates from Black and White input)

Code: Select all

convert Black_and_White.png                         \
-threshold 50% +write x.png                         \
-define connected-components:verbose=true           \
-define connected-components:area-threshold=10      \
-connected-components 4 NULL:
Thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

Is there a way to mix in a single command the 2 steps below to make it more straight forward?
Just concatenate the two commands without the intermediate saved image.

Code: Select all

convert input.png \
-transparent '#39D6AE' \
-transparent '#9EB629' \
-transparent '#8DA718' \
-transparent '#A47721' \
-transparent '#BD8B2B' \
-transparent '#EEEEEE' \
-transparent '#B4CEF8' \
-alpha extract -negate \
-threshold 50% +write x.png                         \
-define connected-components:verbose=true           \
-define connected-components:area-threshold=10      \
-connected-components 4 NULL:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

I think this gets your 7 boxes where I have tossed out the header line and the largest black region which is the background. I tossed out anything smaller than 2000 pixels in area to throw out the white lines surrounding the black boxes.

Code: Select all

convert Black_and_White.jpeg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-define connected-components:mean-color=true \
-connected-components 4 \
null: | tail -n +3 | sed 's/^[ ]*//'

Code: Select all

141: 423x355+530+589 741.0,788.4 131220 gray(0)
140: 422x355+70+589 280.5,788.4 130910 gray(0)
135: 423x205+530+379 741.0,503.4 67770 gray(0)
134: 422x205+70+379 280.5,503.4 67610 gray(0)
28: 423x145+530+203 741.0,275.0 61335 gray(255)
27: 423x145+70+203 281.0,275.0 61335 gray(255)
1: 264x40+680+109 811.5,128.5 10560 gray(255)

Here I just get the 7 bounding boxes:

Code: Select all

convert Black_and_White.jpeg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-define connected-components:mean-color=true \
-connected-components 4 \
null: | tail -n +3 | sed 's/^[ ]*//' | cut -d\  -f2

Code: Select all

423x355+530+589
422x355+70+589
423x205+530+379
422x205+70+379
423x145+530+203
423x145+70+203
264x40+680+109
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Get coordinates Top, Left, size of some areas

Post by cgkas »

Thanks so much both fmw42 and snibgo.

With the second option is not needed to make temp images and the area threshold is great. The sample image is very close to the original. Now I hope this logic works in original image.

Code: Select all

convert input.png -transparent '#39D6AE' \
-transparent '#9EB629' \
-transparent '#8DA718' \
-transparent '#A47721' \
-transparent '#BD8B2B' \
-transparent '#EEEEEE' \
-transparent '#B4CEF8' \
-alpha extract -negate \
-threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-define connected-components:mean-color=true \
-connected-components 4 null: | awk '!/ 0:/{print $2}'

Code: Select all

423x355+530+589
422x355+70+589
423x205+530+379
422x205+70+379
423x145+530+203
423x145+70+203
264x40+680+109
Regards
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

Your awk command is nice and compact, but it still prints the second field of the header line.

(id:
423x355+530+589
422x355+70+589
423x205+530+379
422x205+70+379
423x145+530+203
423x145+70+203
264x40+680+109

You need to skip the first line and the line starting with 0:

So use

Code: Select all

awk 'NR!=1 && !/ 0:/{print $2}'
rather than

Code: Select all

awk '!/ 0:/{print $2}'
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Get coordinates Top, Left, size of some areas

Post by cgkas »

fmw42 wrote: 2018-10-17T13:40:39-07:00 Your awk command is nice and compact, but it still prints the second field of the header line.

(id:
423x355+530+589
422x355+70+589
423x205+530+379
422x205+70+379
423x145+530+203
423x145+70+203
264x40+680+109

You need to skip the first line and the line starting with 0:

So use

Code: Select all

awk 'NR!=1 && !/ 0:/{print $2}'
rather than

Code: Select all

awk '!/ 0:/{print $2}'
Thank you again fmw42 for fix the awk command.

I have 2 questions.

1-) You set a limit to avoit print areas lower than 2000 with "area threshold". Is there a way to limit with a range? let say print for example only areas between 2000 and 30000?

2-) Having the coordinates, I need to create uniques images for those that are at the same Y position.

For example merge 422x355+70+589 with this 423x355+530+589 . I think this could be more complex so, suggest me if is better to open a new post for this.

Thanks again.

Reagards
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get coordinates Top, Left, size of some areas

Post by fmw42 »

1) No, not directly. You would need to extract the area fields and filter on them

2) Again, you would have to filter on Y coordinate of the extracted bounding boxes, then crop the original image for those boxes, one crop command per box. I am not sure I know what you mean by merge? Do you mean include a crop of both areas and the region between them? Or crop each box separately and append them. Perhaps a diagram/picture would help.

All the above would have to be done from a script looping over each row of the output and filtering on the areas and matching the regions of the same or similar Y coordinate of the top left corner.
Post Reply