How to remove black background.

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

How to remove black background.

Post by pat.vachon »

I am trying to remove the background from these receipts I've taken pics of for ProOnGo. I've tried the following with no success.
convert *.jpg -level 10%,100%,1 -trim Resaved.jpg

Before image. http://www.lucascontracting.ca/2013-01-30%20144035.jpg

After image. http://www.lucascontracting.ca/Resaved-0.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

A large fuzz does the trick.

Code: Select all

convert "2013-01-30 144035.jpg" -fuzz 60% -trim t.jpg
snibgo's IM pages: im.snibgo.com
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

That helps but it seems I still have some refinements to make. I only just started using this yesterday so I'm really just tinkering like crazy. Any advice on this set of files.
http://lucascontracting.ca/2013-01-30.rar

I currently have the following command line.
convert *.jpg -level 30%,90%,1 -despeckle -fuzz 40% -trim Resaved.jpg
Increasing the fuzz passed 40% starts to overcrop.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

The exposure varies across the images, so you don't know what levels are best. Instead, I'd use "-contrast-stretch", eg:

Code: Select all

convert in.jpg -colorspace RGB -contrast-stretch 10%%,10%% out.png
And "+repage" might be useful.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

The camera exposure varies, according to the size of the receipt. It's better to use manual control, where the camera has it, so backgrounds and foregrounds are consistent. Of course, a scanner would be even better.

The "black" background has light smudges, needing some heavy manipulation to remove. So I've adopted a two-tiered approach: create a mask, exactly black where the background is roughly black, and transparent elsewhere. Composite the mask over the original, then trim etc.

The (Windows 7) script, a single command, perfectly trims all the files in the rar, and I think does a decent job of ensuring the receipts are readable..

Code: Select all

"%IMG%convert" ^
  %SRC% ^
  ( +clone ^
    -despeckle ^
    -blur 0x5 ^
    -set colorspace RGB ^
    -contrast-stretch 30%%,0%% ^
    -fuzz 40%% -fill black -floodfill 0x0 Black ^
    -fuzz 0 -fill None +transparent Black ^
  ) ^
  -composite ^
  -trim ^
  +repage ^
  -set colorspace RGB ^
  -contrast-stretch 1%%,1%% ^
  out\%2
snibgo's IM pages: im.snibgo.com
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

WOW!!!

K.
1. How do you make a script?
2. Not sure what to set on the camera. I'm using Cam4You Remote. Here is screenshot and help would be much appreciated. http://screencast.com/t/4PysggEC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

Oh. Ah. Um.

A script is a text file that contains commands. How you create one depends on what type of computer you are using -- in particular the operating system. This isn't a scripting forum, and I wouldn't attempt to explain it. There many guides on the net.

Likewise, this isn't a camera forum. Read the manual. If it has a manual mode, that will give you more consistent results.
snibgo's IM pages: im.snibgo.com
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

What is "out\%2" at the end? Also, why the double percent signs?

I have not been able to do this in Win7 as one command line entry. I have tried to do it in two steps and get something close but not at all sharp. http://screencast.com/t/40RTKfBjN1

convert "2013-01-30 144035.jpg" -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) Resaved.jpg

convert "2013-01-30 144035.jpg" -composite Resaved-0.jpg -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% Resaved2.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

Instead of out\%2, just put whatever directory and filename you want. If you are putting this in a Windows batch file, you need to double up the % signs. If you are typing at the command line, don't double them.

I showed it as one command. If you want it as two commands, here they are:

Code: Select all

"%IMG%convert" ^
  %SRC% ^
  -despeckle ^
  -blur 0x5 ^
  -set colorspace RGB ^
  -contrast-stretch 30%%,0%% ^
  -fuzz 40%% -fill black -floodfill 0x0 Black ^
  -fuzz 0 -fill None +transparent Black ^
  mask.png

"%IMG%convert" ^
  %SRC% ^
  mask.png ^
  -composite ^
  -trim ^
  +repage ^
  -set colorspace RGB ^
  -contrast-stretch 1%%,1%% ^
  out\%2
Your first command is okay, except that the mask file should be .PNG, not .JPG. If you use .JPG, the black may not be a nice smooth black, which would make the "-trim" fail. Your second command is somewhat wrong.
snibgo's IM pages: im.snibgo.com
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

YAHOO!!! Got it to work in one step. http://screencast.com/t/PH7JQj622Ce

convert "2013-01-30 144035.jpg" ( +clone -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%,0% -fuzz 40% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%,1% Resaved2.jpg



How can I make this work all files in one directory and create a directory of the cropped images within it?
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

Any ideas on how to make this work on all files in one directory and create a directory of the cropped images within it?
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

Seems like I found the issue.

http://www.imagemagick.org/Usage/basics/#option_list

So is there a work for this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black background.

Post by snibgo »

Any ideas on how to make this work on all files in one directory and create a directory of the cropped images within it?
http://www.imagemagick.org/Usage/basics/#mogrify
snibgo's IM pages: im.snibgo.com
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

I've tried mogrify. It doesn't like -composite as it takes the list of images to do what it does and spits our one image for the entire lot.

This works though. :P

FOR %%G IN (*.jpg) DO convert %%G ( +clone -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% -rotate -90 -set filename:cfilename %%G cropped_%%[filename:cfilename]
pat.vachon
Posts: 9
Joined: 2013-01-30T14:25:30-07:00
Authentication code: 6789

Re: How to remove black background.

Post by pat.vachon »

Well. I thought I was almost ready to put this into production but not quite. The composite is putting into play the blur from the first of the two images. This reduces the image quality significantly on some of the images. Image

FOR %%G IN (Orig_*.jpg) DO convert %%G ( +clone -despeckle -blur 0x1 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% -set filename:cfilename %%G cropped2_%%[filename:cfilename]

How can I create a region to crop instead of cropping after -composite? Sort of like asking -trim for the coordinates, width and height without actually cropping.
Post Reply