Switch to full style
Ask all general Gimp related questions here
Post a reply

Selecting largest contiguous area of a color with python-fu[SOLVED]

Fri Jul 07, 2017 12:31 pm

GIMP Version: 2.8.14
Operating System: Windows
GIMP Experience: Intermediate Level

List any relevant plug-ins or scripts:
gmic



Hello! New to the board, just registered so I could ask this question.

I'm writing a little map generator with python-fu but I'm stumped. I have it generating basic land/sea masks like the one below:

Image

Now I want to separate the ocean from the lakes(black spots within the white landmasses) by selecting the largest contiguous area of black in the image(assuming there will never be a lake bigger than the ocean).Because the map is randomly generated I can't just pick an black x,y coordinate and assume it will fall in the ocean.

Is there a easy way to accomplish this? I have gmic at my disposal as well, if theres anything hidden away there that may help me.

Thanks!

Re: Selecting largest contiguous area of a color with python-fu

Fri Jul 07, 2017 12:38 pm

Determine the total number of pixels in the image (use histogram)
Pick a black point
Use select contiguous
Count again the number of pixels (in this case only for the selection)
If the ratio pixels_of_the_area / total_pixels_of_the_image is less of a threshold you define, consider the area as a lake, otherwise an ocean.

Re: Selecting largest contiguous area of a color with python-fu

Fri Jul 07, 2017 2:39 pm

I would try something like this (let's call this the R'lyeh algorithm):

- Heavy gaussian blur (one third to one half of the image width)
- Threshold from black until you get some black pixels (ie, you try to keep the darkest pixels:
Code:
_,_,_,total,pixels,_=pdb.gimp_histogram(layer, HISTOGRAM_VALUE,0,0)
if pixels>0:

visually, at that point the layer is white with a few black spots, these black spots are the points further from the white in the initial image
- Make a rectangle selection on half the image and count the black pixels again (pdb.gimp_histogram() again. if there are none,continue here, otherwise continue with the other half (it can also be easier to curt in 4 and check the four quadrants). Then continue splitting and counting pixels until you are at a reasonable size (20x20) or as soon as you have all black points (total==pixels).
- Make a contiguous selection using the center of the selection as the starting point.
- You can actually follow all the selections where you find black pixels, and create contiguous selections from each (by adding them). This will cover the case of two disjoint oceans.

Re: Selecting largest contiguous area of a color with python-fu

Fri Jul 07, 2017 6:21 pm

Got it working!

Thanks for the Help!

Re: Selecting largest contiguous area of a color with python-fu[SOLVED

Sat Jul 08, 2017 12:17 pm

Glad to see you've got it working

I was intrigued by this puzzle, and after some thought had an idea, so wrote a script to isolate the ocean
It will work providing the ocean is black and at least one point of the ocean touches the edge of the image, which I assume it always will

The script below can be found at "<Image>/Python/Isolate ocean". It will isolate the ocean as shown in the image below which I ran the script on, (the blue colour was added as an afterthought to check no lakes were selected)

isolate-ocean.zip
(466 Bytes) Downloaded 122 times


The idea is the original 2100 x 2100 image is pasted into a slightly larger 2102 x 2102 image which is itself black, if the fuzzy select tool is used to select any point at the extreme edge of the image it will select black and so all of the ocean

As you have already solved the problem you will have no need for it but I've uploaded it anyway, it may have some curiosity value

steve

mk2.png
mk2.png (126.73 KiB) Viewed 2604 times

Re: Selecting largest contiguous area of a color with python-fu[SOLVED

Sat Jul 08, 2017 3:58 pm

Cool technique, literally thinking "outside of the box". but looking at the code:

1) I think you should use gimp_image_resize() instead if gimp_image_scale()
2) Never, ever use the clipboard operations in your scripts (unless you want to explicitly interact with the user, but this is quite rare). There are function to duplicate layers directly.
3) Once you have the selection, you can delete your additional layer

Re: Selecting largest contiguous area of a color with python-fu[SOLVED

Sat Jul 08, 2017 4:18 pm

interesting method, gotta remember this one.

Re: Selecting largest contiguous area of a color with python-fu[SOLVED

Sat Jul 08, 2017 6:03 pm

ofnuts wrote:Cool technique, literally thinking "outside of the box". but looking at the code:

1) I think you should use gimp_image_resize() instead if gimp_image_scale()
2) Never, ever use the clipboard operations in your scripts (unless you want to explicitly interact with the user, but this is quite rare). There are function to duplicate layers directly.
3) Once you have the selection, you can delete your additional layer

I'm not familiar with gimp_image_resize(), I can't recall if or when I last used it, but looking at it now I can see it's what I should have used

Re the clipboard operations, I wasn't aware using them in scripts was bad practise and I have used them often, I'll look out other options for the future

Thanks for the feedback ofnuts, appreciated
Post a reply