Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connected Components - Wrong Values #574

Closed
TVsIan opened this issue May 30, 2022 · 5 comments
Closed

Connected Components - Wrong Values #574

TVsIan opened this issue May 30, 2022 · 5 comments
Assignees
Labels
Milestone

Comments

@TVsIan
Copy link

TVsIan commented May 30, 2022

I'm trying to write a script that will process bezel images (border with a transparent area in the middle) for an emulator. It extracts the alpha channel to black & white, then runs connected_components to find the white box and turn those coordinates into a text file. But the information in Wand isn't corresponding to what the command line is giving me.

alphaMask

Using magick on the command line results in:

D:\Documents\BezelTest\Bezels>magick alphaMask.png -define connected-components:verbose=true -connected-components 8 test.png
Objects (id: bounding-box centroid area mean-color):
  0: 1920x1080+0+0 957.7,500.3 1.51736e+06 gray(0)
  1: 818x680+556+307 964.5,646.5 556240 gray(255)

Which is correct, the white area's top left corner is at (556, 307) and the size is 818x680.

The part of my script that runs the connected components is:

with Image(filename=join(filePath, "alphaMask.png")) as alphaDetect:
			objects = alphaDetect.connected_components(connectivity=8)

			for cc_obj in objects:
				print('Found area at {} with size {}, {} pixels total, and color {}'.format(cc_obj.offset, cc_obj.size, cc_obj.area, cc_obj.mean_color))

Which outputs:

Found area at (0, 0) with size (1920, 1080), 1517360.0 pixels total, and color gray(0)
Found area at (680, 556) with size (1, 818), 646.5 pixels total, and color undefineda(0,255,255,0)

So the values are all rearranged for the second area, but the first area (the black around the box) is correct.

@emcconville
Copy link
Owner

Thanks for reporting this! Please run python -m wand.version --verbose, and post the output.

@emcconville emcconville self-assigned this May 31, 2022
@emcconville emcconville added this to the Wand 0.6.8 milestone May 31, 2022
@TVsIan
Copy link
Author

TVsIan commented May 31, 2022

Here you go:

Wand 0.6.7
ImageMagick 7.1.0-35 Q16-HDRI x64 ff6dbbb:20220516 https://imagemagick.org

@emcconville
Copy link
Owner

Perfect! That helps a lot.

Looks like a C struct changed 2 months ago. See last change in this commit. The short version is the CCObjectInfo needs ssize_t key appended to the end. In wand, the structures.py should change to something like ...

class CCObjectInfo70A(Structure):
    CCMaxMetrics = CCMaxMetrics
    _fields_ = [('_id', c_ssize_t),
                ('bounding_box', RectangleInfo),
                ('color', PixelInfo),
                ('centroid', PointInfo),
                ('area', c_double),
                ('census', c_double),
                ('merge', c_bool),
                ('metric', c_double * CCMaxMetrics),
                ('key', c_ssize_t)] # <= Only for ImageMagick 0x710

Your welcome to attempt to edit wand/cdefs/structures.py and see if that resolve the issue. I'll need to do a bit more research / review / testing before I push a fix.

@TVsIan
Copy link
Author

TVsIan commented May 31, 2022

That does appear to have fixed it.

@emcconville
Copy link
Owner

This should be resolved in Wand-0.6.8. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants