Gimp-Forum.net
Script to detect and select simple shapes? - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+--- Thread: Script to detect and select simple shapes? (/Thread-Script-to-detect-and-select-simple-shapes)



Script to detect and select simple shapes? - tomatoSauce23 - 09-22-2024

I'd like a plugin to be able to detect and then select simple shapes in a layer or image. So there is a green square in lots of pictures I'm processing and I have to mark them every time they occur, so I do this manually using the Rectangle tool. The squares are always of a similar size and colour. Is there a plugin that could detect squares within a certain size-range and colour-range and then select them? If not, how difficult would it be to write a script like that, given that I have some programming experience but not in Python?


RE: Script to detect and select simple shapes? - Ofnuts - 09-22-2024

I remember answering that question yesterday on GimpChat?


RE: Script to detect and select simple shapes? - PixLab - 09-22-2024

(09-22-2024, 07:08 AM)Ofnuts Wrote: I remember answering that question yesterday on GimpChat?

I saw nothing on GC about that Wink


RE: Script to detect and select simple shapes? - tomatoSauce23 - 09-22-2024

(09-22-2024, 07:08 AM)Ofnuts Wrote: I remember answering that question yesterday on GimpChat?

Hi, this problem is interfering I think...


RE: Script to detect and select simple shapes? - Ofnuts - 09-22-2024

Ok, so the question you are asking is a typical one for computer vision, so maybe you should learn Python (which is very easy if you already have programming experience) and check the openCV library.

With Gimp, I would try this:
  • The color selection can use a RGB color but also tint, hue, saturation, lightness, or any of the RGB channels, all with a threshold.
  • From a selection you can obtain a path. This will rarely be exactly 4 points in a square, even if the shape is rectangular
  • From the path anchors you can determine corners (extremums on X/Y)
  • From these 4 corners you can create a selection a bucket-fill a layer
  • From the initial path you can obtain a selection and bucket-fill another layer
  • You put the top layer in Difference mode and merge the layer. The resulting layer is a map of the differences
  • With the histogram function you can have the pixel count of the difference and see how big that is relative to the square
Now out to hide the pool in my backyard.


RE: Script to detect and select simple shapes? - tomatoSauce23 - 09-22-2024

(09-22-2024, 08:13 AM)Ofnuts Wrote: Ok, so the question you are asking is a typical one for computer vision, so maybe you should learn Python (which is very easy if you already have programming experience) and check the openCV library.

With Gimp, I would try this:
  • The color selection can use a RGB color but also  tint, hue, saturation, lightness, or any of the RGB channels, all with a threshold.
  • From a selection you can obtain a path. This will rarely be exactly 4 points in a square, even if the shape is rectangular
  • From the path anchors you can determine corners (extremums on X/Y)
  • From these 4 corners you can create a selection a bucket-fill a layer
  • From the initial path you can obtain a selection and bucket-fill another layer
  • You put the top layer in Difference mode and merge the layer. The resulting layer is a map of the differences
  • With the histogram function you can have the pixel count of the difference and see how big that is relative to the square
Now out to hide the pool in my backyard.

Thank you. So, I hadn't ever heard of computer vision before, and a quick look suggests to me that the openCV library is a library of programmes written in the common programming languages for various tasks that people have use for. So, am I right to think that you are advising that I go there and see if there is already a programme available to do the task that I'm asking about? and if so, then I guess that all would happen outside of Gimp and inside whichever programming language I choose (Java, for example). So I'd find a programme in the openCV library that would find those squares in my images and crop those images to those squares. 

As for those Gimp instructions, I reckon I could follow them manually... and then are you saying that it would then be possible to write a Python script that does all of those steps as a plugin?


RE: Script to detect and select simple shapes? - Ofnuts - 09-22-2024

(09-22-2024, 08:51 AM)tomatoSauce23 Wrote:
(09-22-2024, 08:13 AM)Ofnuts Wrote: Ok, so the question you are asking is a typical one for computer vision, so maybe you should learn Python (which is very easy if you already have programming experience) and check the openCV library.

With Gimp, I would try this:
  • The color selection can use a RGB color but also  tint, hue, saturation, lightness, or any of the RGB channels, all with a threshold.
  • From a selection you can obtain a path. This will rarely be exactly 4 points in a square, even if the shape is rectangular
  • From the path anchors you can determine corners (extremums on X/Y)
  • From these 4 corners you can create a selection a bucket-fill a layer
  • From the initial path you can obtain a selection and bucket-fill another layer
  • You put the top layer in Difference mode and merge the layer. The resulting layer is a map of the differences
  • With the histogram function you can have the pixel count of the difference and see how big that is relative to the square
Now out to hide the pool in my backyard.

Thank you. So, I hadn't ever heard of computer vision before, and a quick look suggests to me that the openCV library is a library of programmes written in the common programming languages for various tasks that people have use for. So, am I right to think that you are advising that I go there and see if there is already a programme available to do the task that I'm asking about? and if so, then I guess that all would happen outside of Gimp and inside whichever programming language I choose (Java, for example). So I'd find a programme in the openCV library that would find those squares in my images and crop those images to those squares. 

As for those Gimp instructions, I reckon I could follow them manually... and then are you saying that it would then be possible to write a Python script that does all of those steps as a plugin?

Yes to both.