It is currently Thu Jul 04, 2024 4:25 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Selecting largest contiguous area of a color with python-fu[SOLVED]
PostPosted: Fri Jul 07, 2017 12:31 pm  (#1) 
Offline
New Member

Joined: Jul 07, 2017
Posts: 2
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!


Last edited by psychohobo on Fri Jul 07, 2017 6:21 pm, edited 1 time in total.

Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu
PostPosted: Fri Jul 07, 2017 12:38 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
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.

_________________
"Where am I ?"


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu
PostPosted: Fri Jul 07, 2017 2:39 pm  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4756
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:
_,_,_,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.

_________________
Image


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu
PostPosted: Fri Jul 07, 2017 6:21 pm  (#4) 
Offline
New Member

Joined: Jul 07, 2017
Posts: 2
Got it working!

Thanks for the Help!


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu[SOLVED
PostPosted: Sat Jul 08, 2017 12:17 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 199
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)

Attachment:
isolate-ocean.zip [466 Bytes]
Downloaded 121 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

Attachment:
mk2.png
mk2.png [ 126.73 KiB | Viewed 2595 times ]


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu[SOLVED
PostPosted: Sat Jul 08, 2017 3:58 pm  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4756
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

_________________
Image


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu[SOLVED
PostPosted: Sat Jul 08, 2017 4:18 pm  (#7) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
interesting method, gotta remember this one.

_________________
TinT


Top
 Post subject: Re: Selecting largest contiguous area of a color with python-fu[SOLVED
PostPosted: Sat Jul 08, 2017 6:03 pm  (#8) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 199
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


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) use of a color curves preset in a python filter

42

No new posts Selecting outline of image

2

No new posts fuzzy select not fully selecting

4

No new posts Attachment(s) Script for selecting, cropping the selection and saving the file

8

No new posts In area 51

4



* Login  



Powered by phpBB3 © phpBB Group