Gimptalk - Premier Gimp Community: How to select blue lines that are within a black rectangle? - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to select blue lines that are within a black rectangle?

#1 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 27 July 2010 - 04:54 PM

Hi-
I have an image with a white background. The image is made up of a black rectangle (i.e. black lines that form a rectangle, white background within the rectangle). I have various blue lines within the rectangle.

My objective is to select everything WITHIN the lines of the rectangle (I don't want any trace whatsover of the black lines).

I have written a script to
1) do a fuzzy select on the white background within the rectangle; this gives me a selection with holes in place of the blue lines
2) save the selection to a selection mask
3) do a fuzzy selection on the selection mask using a NON-selected pixel as my seed - this selects all pixels outside the PERIMETER of my original fuzzy selection from step (1)
4) Invert the fuzzy selection from step (3); this selects everything WITHIN the lines of the rectangle (i.e. now I don't have the "holes" that I did in my original selection from step (1))

This script works perfectly for my needs EXCEPT when a blue line is touching the rectangle, in which case, that blue line is excluded from my final selection.

For days now, I've been trying to figure out how to include that blue line up until the point where it touches the rectangle, but am too much of a noob to solve it...

Would really be grateful for some help!

Thanks-
Sam
0

#2 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 306
  • Joined: 01-June 05
  • LocationUK

Posted 27 July 2010 - 10:16 PM

Have you tried selecting the black line using the select by colour tool, cut the selection then autocrop the image?

Kevin
0

#3 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 28 July 2010 - 01:31 PM

Hi Kevin!

Thanks for your post. The image is scanned, so the black line is not exactly black and appears irregularly feathered. I spent about 30 minutes trying to find a color / threshold that was reliable, but when a blue line touches the black, some of the dark blue pixels in the line get selected too (when using fuzzy select tool). I am a complete noob when it comes to working with images and colors, so maybe there is a reliable way to do it... Also, I gave a simplified example - there are actually multiple such rectangles on each page... sorry!

Options I was thinking of:
1) Assuming I could find a reliable way to select just the lines of the black rectangle, is there a way to replace the outer edges of the selection with a 1 px wide white line? Please keep in mind that the selection will be skewed and ragged, even after deskewing). Then my original 4 step process would work fine!

2) After selecting the lines of the black rectangle, is there a way to select the INTERIOR of that selection, i.e. those pixels that are bounded by the selection i.e. the white box?

3) Is there a way to extrapolate the WHITE box within the black rectangle by bridging the gaps formed by any anomolies / holes that affect the perimeter of the white box, e,g, when a blue line is touching a black line so that the fuzzy select also selects parts of the touching blue line which now destroys the integrity of the white box.

Ah... so much to learn...
0

#4 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 306
  • Joined: 01-June 05
  • LocationUK

Posted 28 July 2010 - 02:15 PM

Hi Sam.

Is there some reason you can't post an example image? it would make it so much easier to understand your problem.

I tried guessing and drew an image using the airbrush tool with a black rectangle and a dark blue line inside. I found that if I decompose the image into HSV layers then the black line disappears in the H and S layers because it has no Hue or Saturation values, BUT because you are working with scanned images, there's probably going to be colour noise in the black (and white).

Kevin
0

#5 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 28 July 2010 - 07:31 PM

>> if I decompose the image into HSV layers then the black line disappears in the H and S layers because it has no Hue or Saturation values

WOW! I wish I knew what you knew! Way above my payscale!

Thanks for your efforts. Much appreciated!

http://www.sabboushi...ushi/Sample.tif

http://www.sabboushi...ushi/Sample.tif
0

#6 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 28 July 2010 - 09:35 PM

PS - the white background is expendable if that helps; it's just the blue that I care about :)
0

#7 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 306
  • Joined: 01-June 05
  • LocationUK

Posted 29 July 2010 - 01:14 PM

Thanks for the example file, it certainly helps to see your problem.

There seems to be a significant amount of blue in the "black" lines, which is making it difficult to devise a scheme to isolate the wanted blue lines.

At the moment I'm thinking to try some noise reduction to get the black lines smoother before removing them.

The idea being to remove the black lines, autocrop to the edges of the blue lines then use the coordinates of that area to define a selection area on the original image - hopefully just leaving the blue lines.

Kevin
0

#8 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 689
  • Joined: 05-November 07

Posted 29 July 2010 - 01:34 PM

Hi-

Since you are scripting this anyway, start with what you have

"1) do a fuzzy select on the white background within the [1 of the] rectangle[s]; this gives me a selection with holes in place of the blue lines"

Then get the selection bounds (gimp-selection bounds )

Then do a rectangle selection using those bounds.

This will really depend on how well aligned the image is, and will (possibly) leave a thin black fringe in certain areas that can be removed by shrinking the selection a few pixels on each size when making the rectangle selection. You also might want to pre-process it using the deskew plugin (the source of which seems to have disaperard, but there is a windows binary attached here: http://registry.gimp.org/node/22177 ) before selecting and obtaining the selection bounds.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#9 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 29 July 2010 - 03:06 PM

Kevin- thanks for your continued efforts.

Rob! I feel I know you from all your posts and scripts in various GIMP related forums! Learned a lot from you - thanks!

In large part because of your previous posts, my current script is EXACTLY what you just suggested - I am deskewing (which still often leaves a several pixel skew in the black (feathered) line). The selection bounds is unfortunately the upper left selected pixel which means I often get a "thin black fringe" towards the right when the upper right selected pixel is lower (hope that made sense). I was cropping 4 px to eliminate, but this often cropped too much blue.

Or if the left is lower than the right, then I end up with no black in the selection bounds, but the selection bounds has already cropped some of the blue, and then I crop another 4px... still losing too much blue.

Hence, my post.

I really appreciate you taking the time to respond - I'm thinking there has to be a way to do this without losing blue-- still hoping to get some feedback on the 3 options I proposed. It would be helpful for me to know if any of the options I suggested are NOT possible in GIMP and "why not?" to help me better understand the limitations of GIMP.
0

#10 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 689
  • Joined: 05-November 07

Posted 29 July 2010 - 05:27 PM

hmmmm-

Playing with it I came up with a pretty good method. (how perfect does it have to be?)

After the deskew and crop to rectangle (call this image A) decompose the result to CMYK in layers.

Delete the M and Y layers. Set the C layer to subtract. Copy Visible and paste that as a new layer in your image A and set its mode to subtract.

This seems to remove almost all the black line without having a deleterious impact on the pen.

Alternately, if you don't care about deterioration or preservation of then actual pen colour, decompose to CMYK, fill the K layer with black then recompose. This will get rid of all the black, but also lighten up the image.

Lastly I had luck decomposing to CMYK, deleting Y and K, thresholding C and M so that the border noise was gone (around 64) then setting the top to screen. Copy visible, paste as a layer mask then dilate the mask to make more visible

Regarding your option questions,
1) it is he same problem - getting only the "outside" of the selection. My divide scanned images script converts a selection into a path then iterates the paths. You could do something like that to find the "outer" path then stroke that...maybe.

2) if you save the selection as a channel, you can magic wand select that in the interior area.... not sure if that is what you want to do or not.

3) you could try and find the four corners by sampling points in this order (sample for upper left hand side):

1247
358
69
.

until you find the "white" pixel. Repeat on the other three corners then(gimp-free-select) with those 4 points. Then invert and fill with white.

Good luck! Let us know what you come up with.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#11 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 01 August 2010 - 09:31 PM

>> At the moment I'm thinking to try some noise reduction to get the black lines smoother before removing them. The idea being to remove the black lines, autocrop to the edges of the blue lines then use the coordinates of that area to define a selection area on the original image - hopefully just leaving the blue lines.

Kevin - your idea sounds great. Am eager to hear back if you have the time to explain to me how to do that.


Rob A: Holy Cow Batman: it took me hours of reading documentation to understand how to even TRY what you suggested!

I was unable to achieve the results you describe, so I suspect I misunderstood some of your steps. Here is exactly what I did:

1) Opened sample.tif
2) Colors, Components, Decompose:
Color Model: CMYK
Decompose to layers: checked
Foreground as registration color: unchecked
3) Delete M and Y layers of new image. Set Mode of C layer to "Subtract"
NICE!! ALL the blue disappears! I am left with white lines and white numbers (i.e. whatever is "black" in sample.tif is now "white" in this image)
4) Edit, Copy Visible
5) In sample.tif, Edit, Paste As, Layer
6) Set pasted layer Mode to "Subtract".
My black lines now appear more saturated!

I was unable to get good results from your other options. I hope you might point out what steps I did incorrectly for the first option... I suspect I might then be able to correctly follow your other options too!

I appreciate the time you've spend playing with this and responding to my post. Thank you!
0

#12 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 306
  • Joined: 01-June 05
  • LocationUK

Posted 02 August 2010 - 03:06 PM

Hi Sam,

ignore what I was saying before, I've played with Rob's process (which doesn't work for me either) but I've tried something else with the process.

Decompose to CMYK with the "Decompose to layers" checkbox unchecked and close the resulting Magenta, Yellow and Black images so you've only got the Cyan image remaining.

Use the Colours->Curves tools to make the black background, and as much of the lines as possible, black, and the white areas as white as possible - i.e. an extreme s-curve. Alternatively use the Colours->Threshold tool (I found the value of 32 to be good for the sample image).
Edit->Copy Visible

Back on the original - on the layers dialog, right click on the layer and select "Add Layer Mask"
Then Edit->Paste and anchor the floating selection.

You should now have the blue on a transparent background.
Add a new white layer and move it under the wanted layer if you want a white background.


Kevin
0

#13 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 03 August 2010 - 12:35 AM

Thanks Kevin - really nice of you to spend the time!

I tried the curves approach; the resulting images I created lost too many of the darkest blue pixels...

For the threshold approach, I did not lose as much blue at threshold 63, 255 (probably because I was able to duplicate your steps -- probably not so with the curves). If you got better results than what I've described, please encourage me to take another look - maybe I'm just not doing a good job following your steps.

I'm working on a different approach that is a combination of my original approach (fuzzy selection) on the black background of a merged magenta-subtract-from-black-components layer that gives just about perfect results.

Thanks again for all the time and effort you put into this - I've learned a lot!
0

#14 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 689
  • Joined: 05-November 07

Posted 03 August 2010 - 04:23 PM

SAbboushi said:

Rob A: Holy Cow Batman: it took me hours of reading documentation to understand how to even TRY what you suggested!

I was unable to achieve the results you describe, so I suspect I misunderstood some of your steps. Here is exactly what I did:

1) Opened sample.tif
2) Colors, Components, Decompose:
Color Model: CMYK
Decompose to layers: checked
Foreground as registration color: unchecked
3) Delete M and Y layers of new image. Set Mode of C layer to "Subtract"
NICE!! ALL the blue disappears! I am left with white lines and white numbers (i.e. whatever is "black" in sample.tif is now "white" in this image)
4) Edit, Copy Visible
5) In sample.tif, Edit, Paste As, Layer
6) Set pasted layer Mode to "Subtract".
My black lines now appear more saturated!

I was unable to get good results from your other options. I hope you might point out what steps I did incorrectly for the first option... I suspect I might then be able to correctly follow your other options too!

I appreciate the time you've spend playing with this and responding to my post. Thank you!



I must have been asleep when I wrote that up.

Not subtract. Addition. (That will fade it.) duplicate this layer again (or adjust the levels grey value down) and it should get rid of all the black.


Again, with regradrs to the rest, I seems to have fluff in my head... so please disregard... :)

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#15 User is offline   SAbboushi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 38
  • Joined: 29-June 10

Posted 03 August 2010 - 06:17 PM

K thanks - I will try that :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic