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

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

Post by cgkas » 2018-10-17T16:13:27-07:00

fmw42 wrote:
2018-10-17T15:35:25-07:00
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.
Ok. For the question #2 an example below.

Let say I already have a way to identify the first 2 images by their coordinates (same Y value). I would like to generate an image with the Box1 and Box2 joined with lets say 15 pixel between them and 2 pixel border each box of color red or black. Something like this:

Box1Box2.png
Image

And for Box3 and Box4 would be like this:

Box3Box4.png
Image

User avatar
fmw42
Posts: 25402
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 » 2018-10-17T17:00:14-07:00

You can extract the boxes as you desire, match Y coordinates, then crop each box from the input and join them with a white space between them using +smush +15+0. It is like +append, but allows spacing or overlap. Alternately, you could combined them with montage, as it allows the -geometry +15+0 to put the space between them.

See
https://imagemagick.org/script/command- ... .php#smush
https://imagemagick.org/Usage/montage/

User avatar
fmw42
Posts: 25402
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 » 2018-10-17T17:05:30-07:00

P.S. You should be able to use the unix tool, sort, to sort them by the upper left Y and X coordinates so that you can then search linearly for neighboring entries with similar Y values. You may have to make several passes of the box coordinate list. I have not given it too much thought. Perhaps the multiple passes are not needed, if you save boxes with similar Y values to a new list or array until you get to the first one that is not similar in Y. Then start a new list or array. Etc.

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 » 2018-10-17T17:08:43-07:00

Thanks so much fmw42.

I'll follow you suggestions and links shared.

User avatar
fmw42
Posts: 25402
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 » 2018-10-18T19:59:04-07:00

Here is unix bash shell code to do what you want.

Input:
Image

But since your input was converted from PNG to JPG you original colors were modified. So I have to start with the black/white extracted image you provided:

Image

I use CCL to extract the bounding boxes and sort by increasing Y. I then test if Y values of successive boxes are within 5 pixels and create a list that concatenates \( mpr:img -crop ${bboxArr[0]} +repage \). In the actual convert code, I read the input into mpr: in-memory and delete the original, so that I can crop the mpr:img multiple times. Then I test if only one set of crop values in the list, I just output the crop. But if two or more, I use +smush to append them with a 15 pixel white space. At the end I have to test for the last created set and convert that.

list=""
bboxArr=(`convert BW.jpeg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-connected-components 4 \
null: | awk 'NR>2 {print $2}' | sort -g -t "+" -k 3,3 -k 2,2`)
num="${#bboxArr[*]}"
j=0
list=" \( mpr:img -crop ${bboxArr[0]} +repage \) "
yy=`echo "${bboxArr[0]}" | cut -d+ -f3`
for ((i=1; i<num; i++)); do
ynew=`echo "${bboxArr[$i]}" | cut -d+ -f3`
test=`convert xc: -format "%[fx:abs($ynew-$yy)<=5?1:0]" info:`
if [ $test -eq 1 ]; then
list="$list \( mpr:img -crop ${bboxArr[$i]} +repage \) "
else
count=`echo "$list" | wc -w`
if [ $count -eq 6 ]; then
eval 'convert input.jpeg +write mpr:img +delete '$list' image$j.jpg'
else
eval 'convert input.jpeg +write mpr:img +delete '$list' -background white +smush +15+0 image$j.jpg'
fi
list=" \( mpr:img -crop ${bboxArr[$i]} +repage \) "
ynew=$yy
yy=`echo "${bboxArr[$i]}" | cut -d+ -f3`
j=$((j+1))
fi
done
if [ $count -eq 6 -a $i -eq $num ]; then
eval 'convert input.jpeg +write mpr:img +delete '$list' image$j.jpg'
elif [ $count -ne 6 -a $i -eq $num ]; then
eval 'convert input.jpeg +write mpr:img +delete '$list' -background white +smush +15+0 image$j.jpg'
fi

Image0
Image

Image1
Image

Image2
Image

Image3
Image

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 » 2018-10-19T10:21:44-07:00

Thank yout for help and time fmw42 and that great script., but I;m not sure why for me is not working correctly.

The image0 is the same as yours. Your image1 is created as image3 in my side. and the boxes are generated in individual images (image1, 2, 4 and 5). Additionally is creating the images with the top region where it says "Box 1, Box2, etc" I want that top regions cut off of output if possible.

Thanks again.

User avatar
fmw42
Posts: 25402
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 » 2018-10-19T11:02:34-07:00

In what way does the script not work? Have you tried just using the BW image I used and my code? What do you get for the results?

To remove the red tops, you would need to modify your processing to keep them as part of the black background in your BW image.

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 » 2018-10-19T11:49:42-07:00

Below you see that I have the BW.JPEG and input.JPEG and the images generated are different than yours. For example Box1 is not joined with Box2
Image

I attach again input in JPEG
Image

User avatar
fmw42
Posts: 25402
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 » 2018-10-19T12:10:16-07:00

What is your Imagemagick version and platform?

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 » 2018-10-19T12:24:33-07:00

$ convert -version
Version: ImageMagick 6.9.10-11 Q16 x86_64 2018-09-08 https://www.imagemagick.org


And testing on Cygwin.

User avatar
fmw42
Posts: 25402
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 » 2018-10-19T12:39:37-07:00

You have not said what platform/OS!

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 » 2018-10-19T13:34:31-07:00

fmw42 wrote:
2018-10-19T12:39:37-07:00
You have not said what platform/OS!
I'm running the bash commands on Cygwing (like a linux terminal emulator) under Windows 7.

User avatar
fmw42
Posts: 25402
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 » 2018-10-19T16:37:35-07:00

Do you have all the unix tools that I used installed such as AWK, sort, cut, wc, eval. If not, then install them. If so, then perhaps Cygwin behaves differently with these tools. I do not use Cygwin, so cannot confirm.

Perhaps use snibgo can test my command on his Cygwin, if he has time and interest?

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 » 2018-10-19T16:42:08-07:00

Yes. I have all those tools.

I'll try to test on a ubuntu machine, but at the end the final solution I will need to run it on cygwin. So. I'll need understand the logic in your code.

User avatar
fmw42
Posts: 25402
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 » 2018-10-19T17:10:44-07:00

My best guess is that your sort does not work the same as mine. Test this:

Code: Select all

convert BW.jpeg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=2000 \
-connected-components 4 \
null: | awk 'NR>2 {print $2}' | sort -g -t "+" -k 3,2
It should list this in the following order:

Code: Select all

264x40+680+109
423x145+530+203
423x145+70+203
422x205+70+379
423x205+530+379
422x355+70+589
423x355+530+589
I explained the logic above. If you have specific questions, then ask away.

Post Reply