Skip to main content

[JAI] Remove white from image (trim())

7 replies [Last post]
Anonymous

Hi.
I have serveral product images, that has white backgound. Using Photoshop or
ImageMagick, I'm used to trim the images.
The area I'd like to trim to is (RGB(255,255,255)).
Are there any way to do this using JAI?
--
View this message in context: http://www.nabble.com/Remove-white-from-image-%28trim%28%29%29-tf4354379...
Sent from the JAI Projects - Interest mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

Thanks for Rafael and Simone for their suggestions. Here is another one.

// Extract bands
band0 = BandSelect(src, 0)
band1 = BandSelect(src, 1)
band2 = BandSelect(src, 2)

// Derive grayscale image with pixel = 255 <=> RGB=(255,255,255)
anded = And(band0, band1, band2)

// Map [0,254]->0 and 255-> 1
binarized = Binarize(anded, 255)

// Map all pixels with a 0-valued 8-neighbor to 0
eroded = Erode(binarized, {{1,1,1},{1,1,1},{1,1,1}})

// Map 0->0 and 1->255
mask = Rescale(eroded, 255, 0)

// Clear pixels where mask value is 255.
band0 = Xor(band0, mask)
band1 = Xor(band1, mask)
band2 = Xor(band2, mask)

// Merge the bands to get the destination.
trimmed = BandMerge(band0, band1, band2)

The problem with this approach is that the a bit of backgroun will remain
around the edges of the object and if there is any white inside the target it
could get trimmed as well.

On Thu, 30 Aug 2007, kriskross wrote:

> Hi.
> I have serveral product images, that has white backgound. Using Photoshop or
> ImageMagick, I'm used to trim the images.
> The area I'd like to trim to is (RGB(255,255,255)).
> Are there any way to do this using JAI?
> --
> View this message in context: http://www.nabble.com/Remove-white-from-image-%28trim%28%29%29-tf4354379...
> Sent from the JAI Projects - Interest mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Simone Giannecchini

Ciao,
a possible way to do this could be as follows:

source image --> band merge to get 1 band (for example if you have RGB
you could do (R+G+B)/3 ) --> invert to respect ROI inclusion rule-->
build a ROI with a suitable threshold value.

You can then get the bounds of the ROI and crop the original image.

It is a bit complex for sure (and maybe I have also put some mistakes
in it :-) ) but it would teach you how to use JAI along the road to
the solution.

Hope that helps,
Simone.

On 8/30/07, kriskross wrote:
>
> Hi.
> I have serveral product images, that has white backgound. Using Photoshop or
> ImageMagick, I'm used to trim the images.
> The area I'd like to trim to is (RGB(255,255,255)).
> Are there any way to do this using JAI?
> --
> View this message in context: http://www.nabble.com/Remove-white-from-image-%28trim%28%29%29-tf4354379...
> Sent from the JAI Projects - Interest mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

--
-------------------------------------------------------
Eng. Simone Giannecchini
President /CEO GeoSolutions S.A.S.
Via Carignoni 51
55041 Camaiore (LU)
Italy

phone: +39 0584983027
fax: +39 0584983027
mob: +39 333 8128928

http://www.geo-solutions.it

-------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Rafael Santos

kriskross
> I have serveral product images, that has white backgound. Using Photoshop or
> ImageMagick, I'm used to trim the images.
> The area I'd like to trim to is (RGB(255,255,255)).
> Are there any way to do this using JAI?
>
Yes.

OK, I'll give more hints: :-)

Scan the borders locating the first non-white pixel to discover the
bounding rectangle of the image, and do a crop/translate operation.
Examples that may help are on jaistuff.dev.java.net

Rafael

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

kriskross

rafael.santos wrote:
>
> kriskross
>> I have serveral product images, that has white backgound. Using Photoshop
>> or
>> ImageMagick, I'm used to trim the images.
>> The area I'd like to trim to is (RGB(255,255,255)).
>> Are there any way to do this using JAI?
>>
> Yes.
>
>
> OK, I'll give more hints: :-)
>
> Scan the borders locating the first non-white pixel to discover the
> bounding rectangle of the image, and do a crop/translate operation.
> Examples that may help are on jaistuff.dev.java.net
>
> Rafael
>
Found some examples at jaistuff, but no on how to scan borders for non-white
pixel.
Do you have any more hints?
The prosess sounds correct.
--
View this message in context: http://www.nabble.com/Remove-white-from-image-%28trim%28%29%29-tf4354379...
Sent from the JAI Projects - Interest mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Rafael Santos

>> Scan the borders locating the first non-white pixel to discover the
>> bounding rectangle of the image, and do a crop/translate operation.
>> Examples that may help are on jaistuff.dev.java.net
>>
>>
> Found some examples at jaistuff, but no on how to scan borders for non-white
> pixel.
> Do you have any more hints?
>
Yes. The examples show how you can get a pixel's RGB value for a
coordinate.
To find the first pixel from the top which is not white use the algorithm:
int topMargin = 0;
for(row=0;row {
for(col=0;col {
get pixel at col,row
if it is not white
{
topMargin = row
break
}
}
}

Repeat modifying scan directions to locate the bottom, left and right
margins.
Rafael

[att1.html]

kriskross

rafael.santos wrote:
>
>
>>> Scan the borders locating the first non-white pixel to discover the
>>> bounding rectangle of the image, and do a crop/translate operation.
>>> Examples that may help are on jaistuff.dev.java.net
>>>
>>>
>> Found some examples at jaistuff, but no on how to scan borders for
>> non-white
>> pixel.
>> Do you have any more hints?
>>
> Yes. The examples show how you can get a pixel's RGB value for a
> coordinate.
> To find the first pixel from the top which is not white use the algorithm:
> int topMargin = 0;
> for(row=0;row > {
> for(col=0;col > {
> get pixel at col,row
> if it is not white
> {
> topMargin = row
> break
> }
> }
> }
>
> Repeat modifying scan directions to locate the bottom, left and right
> margins.
> Rafael
>
Tried for a fiex hours now. I'm having trouble understanding who to get the
pixel.

But I'm opening the image using:
File input = new File("C:/uzipTest/testIMG.tif");
File output = new File("C:/uzipTest/bilde.jpg");
RenderedImage src = (RenderedImage)JAI.create("fileload",
input.getAbsolutePath());
But I can not find any suitable method to :
1.get the pixel 0,0
2. get the color for the pixel
--
View this message in context: http://www.nabble.com/Remove-white-from-image-%28trim%28%29%29-tf4354379...
Sent from the JAI Projects - Interest mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Rafael Santos

Kriskross
> Tried for a fiex hours now. I'm having trouble understanding who to get the
> pixel.
>
> But I'm opening the image using:
> File input = new File("C:/uzipTest/testIMG.tif");
> File output = new File("C:/uzipTest/bilde.jpg");
> RenderedImage src = (RenderedImage)JAI.create("fileload",
> input.getAbsolutePath());
> But I can not find any suitable method to :
> 1.get the pixel 0,0
> 2. get the color for the pixel
>

Did you read the examples in https://jaistuff.dev.java.net/ ? They could
save you some hours.

Rafael

[att1.html]