Alpha channel in OpenCV - merge

does OpenCV support alpha-channel? Or is there any way to work with transparent png? I need to merge two images, where the first one is background and the second one is image which was rotated by cvWarpAffine. I can do this by merging pixels one by one and omit pixels with some value, which I set in cvScalar in cvWarpAffine. However, I don't think that this is intended solution.
Thanks for suggestions

Updated answer: Use CV_LOAD_IMAGE_UNCHANGED flag to load all four channels (including Alpha) from the image. Then, use mixChannels() and/or split() to separate the alpha channel from others, and threshold on it, as explained below.
Very old answer:
OpenCV does not support alpha channel, only masking. If you want to read in PNG with alpha, use imagemagick first to extract alpha channel:
convert input.png -channel Alpha -negate -separate input-mask.png
Then in OpenCV you can do sth like this:
Mat_<float> mask = imread("input-mask.png", 0);
threshold(mask, mask, 254., 1., THRESH_BINARY);
... to get a real mask (usable as mask matrix in OpenCV operations). Or you can use it in your own operations without the thresholding. To apply the mask it may also be a good idea to extend it to three channels:
std::vector<Mat> marr(3, mask);
Mat_<Vec<float, 3> > maskRGB;
merge(marr, maskRGB);
After that you can test it like this:
imshow("Target", target);
imshow("Mask", mask*255);
imshow("Applied", target.mul(maskRGB));
waitKey();
Note: This is OpenCV 2.0 code.

Here is a bash script that I threw together that will perform the ImageMagick conversion given by ypnos on all of the png files in a directory. You can make it recursive by replacing the * in the third line with a find command.
#!/bin/bash
for file in *
do
if [[ $file =~ (.+)-mask\.png ]]; then
echo "Ignoring mask $file"
elif [[ $file =~ (.+)\.png ]]; then
echo "Generating mask for $file"
basefn=${BASH_REMATCH[1]}
convert "$basefn.png" -channel Alpha -negate -separate "$basefn-mask.png"
fi
done

Related

How can I merge a signature and my module with an ImageMagick command?

I have this starting situation.
I have a jpg picture (A) with a module in which the signature slot is always in the same place.
I have also a signature (B) jpg picture, separated.
I use a program that allows external calls, but one single line only.
Is there an ImageMagick single command line command that allows me to do
Put Image (B) into image (A) at a specific x,y,height and width
Consider WHITE #FFFFFF as the alpha channel for image (B)
?
I am looking since a while now but haven't figured anything out.
Thanks
In ImageMagick, you would first make white transparent and then overlay B onto A. You need to use parentheses processing to keep the processes separate but in the same command line.
Unix syntax for ImageMagick 6:
convert imageA \( imageB -transparent white \) -geometry WxH+X+Y -compose over -composite result
or possibly
convert imageA \( imageB -transparent white -resize WxH \) -geometry +X+Y -compose over -composite result
Best if you provide your input images for demonstration.
See
https://legacy.imagemagick.org/Usage/basics/#parenthesis
https://legacy.imagemagick.org/Usage/compose/#compose
https://legacy.imagemagick.org/Usage/windows/

Imagemagick commands to resize, rotate, wrap, and combine images

I'm currently using Photoshop to resize, rotate randomly, and wrap images randomly to create this type of montage....
Comic Covers
I got to thinking that kind of thing should be doable in Imagemagick. I know how to use all of the commands separately, and I can do random rotations and wraps using BASH, but getting a single image out of individual images is eluding me.
Assume that the source pictures are different sizes but should be resized to 250px wide. The images will be named image1.jpg, image2.jpg, etc. Also assume that the destination should be 1000x1000px. Depending on how many pictures I have, the whole 1000x1000 image may not be covered - I understand this. I mainly use BASH, but I have several different environments and shells available to me.
Using ImageMagick 6 or 7, if you have enough memory to read in all your images at once you can resize them, randomly rotate them, and place them all in random locations on a 1000x1000 canvas with a command like this...
convert granite: -duplicate 11 -resize 250x \
-background none -gravity center -extent 1000x1000 \
-distort SRT "%[fx:rand()*45-22.5]" -virtual-pixel tile \
-distort affine "%[fx:w/2],%[fx:h/2] %[fx:rand()*w],%[fx:rand()*h]" \
-flatten result.png
That uses the ImageMagick built-in image "granite:" duplicated 11 more times. Replace "granite: -duplicate 11" with a list of your input files.
It starts by resizing them all to 250 pixels wide, then placing them each in the center of a 1000x1000 transparent canvas.
The real work is done in the distort operations. First "-distort SRT" rotates each image a random amount between -22.5 and +22.5 degrees. Then the "-distort affine" relocates each image to a random location within the canvas. Any part of an image going beyond the canvas will be rolled back into the opposite side. That makes the result suitable for tiling.
This command flattens everything onto a transparent background wherever it might show between the images. Add "-background blue" just before the "-flatten" operation to change the background to blue, for example.
This works on my IM 6 in bash. For IM 6 in Windows change the continued line backslashes "\" to carets "^". For IM version 7 change "convert" to "magick".
Here is a bash Imagemagick 6 script that takes a list of images. You can replace it with your images. It uses subshell processing to avoid needing to write temporary images to disk. It saves the images in a miff: format as one file from the loop. Then it pipes the multipage miff: file to -layers merge, which overlays the images onto the 1000x1000 transparent base image. For Imagemagick 7, replace convert with magick.
list="lena.jpg barn.jpg mandril3.jpg zelda1.jpg"
convert -size 1000x1000 xc:none result.png
(
for img in $list; do
angle=`convert xc: -format "%[fx:round(-22.5+45*(rand()))]" info:`
xoff=`convert xc: -format "%[fx:round(1000*rand())]" info:`
yoff=`convert xc: -format "%[fx:round(1000*rand())]" info:`
#echo >&2 "angle=$angle; xoff=$xoff; yoff=$yoff"
convert "$img" -resize 250x -background none -rotate $angle -set page +${xoff}+${yoff} miff:-
done
) | convert result.png - -layers merge +repage result.png
If you have enough resources to hold all the images at once, then you can also do it in one command line as follows:
convert -size 1000x1000 xc:none \
\( lena.jpg barn.jpg mandril3.jpg zelda1.jpg -virtual-pixel none -background none \
+distort SRT "0,0 %[fx:250/w] %[fx:-22.5+45*rand()] %[fx:rand()*1000],%[fx:rand()*1000]" \) \
-layers merge +repage result.png
Cool, I'll try fmw42's script, but this is a script I came up with. It generates temporary files (which it deletes) and several convert commands, but it does work....
# Create blank montage...
convert -size 750x750 xc:black montage.jpg
for file in $(ls hall*.jpg | grep -v halloweencovers.jpg); do
echo $file
angle=$RANDOM; let "angle %= 32"; let "angle = angle - 16"; let "angle = angle * 5"
offsetx=$RANDOM; let "offsetx %= 75";let "offsetx = offsetx * 10"; offsetx="+$offsetx"
offsety=$RANDOM; let "offsety %= 75";let "offsety = offsety * 10"; offsety="+$offsety"
# Create blank image...
convert -size 750x750 xc:transparent blank.png
# create 250px image and rotate....
convert $file -resize 250x -alpha set -background none -rotate $angle out.png
# add 250px image to blank 750x750 canvas
convert blank.png out.png -composite output.png
# offset and wrap blank canvas with output image
convert output.png -roll ${offsetx}${offsety} output2.png
# merge montage with offset image
convert montage.jpg output2.png -composite montage.jpg
# clean up
rm -f out.png output.png output2.png blank.png
done

Connect pixels in matlab

Please suggest how to connect the dotted pixels in an image like below:
Original Image
I want to apply OCR on this image. I have tried some morphological operations such as thickening and bridging but not obtaining the correct output as expected (NH5343320).
The original image is also uploaded. On applying horizontal edge detection on the original image, I got the dotted image as above. Is there any another methods available for applying OCR in these kind of images.
I would crop out and fill in a template for each of the available letters. Presumably, that would be the letters [A-Z] and the digits [0-9] like this.
0.png
3.png
Now I would do a sub-image search for each of them in your original image. I am doing this at the command-line with ImageMagick but you could use Matlab, OpenCV, or CImg or the Python, Perl, PHP, C, C++ bindings of ImageMagick.
So, I look for the 3 first:
compare -metric rmse -dissimilarity-threshold 1 -subimage-search plate.png 3.png result.png
25607.9 (0.390752) # 498,46
So, the 3 is found at coordinates 498,46. There will be 2 output files, output-0.png which looks like this:
and output-1.png in which you can see the brightest areas showing where the match is best:
Likewise with the 0:
compare -metric rmse -dissimilarity-threshold 1 -subimage-search plate.png 0.png result.png
31452.6 (0.479936) # 664,44

Tesseract and tiff format - spp not in set {1,3}

While trying to run this command:
tesseract bond111.tif bond111 batch.nochop makebox
I get the next error
Error in pixReadFromTiffStream: spp not in set {1,3}
Error in pixReadStreamTiff: pix not read
Error in pixReadTiff: pix not read
Assuming that spp not in set is the main error here, what does it mean?
At first it had trouble because the bpp was higher than 24 so I reduced it using Gimp but that did not resolve the issue.
It probably means your TIFF image has an alpha channel and therefore the underlying Leptonica library used by Tesseract doesn't support it. If you're using Imagemagick then be aware that operations such as -draw can cause alpha channels to be added. If you're using convert in your workflow and want to remove the channel again immediately, flatten the image before writing by adding -background white -flatten +matte before the output filename, e.g.:
convert input.tiff -fill white -draw 'rectangle 10,10 20,20' -background white -flatten +matte output.tiff
Tesseract (well, Leptonica) accepts PNGs these days and is less picky about them, so it might be easier to migrate your workflow to PNG anyway.
Sources: magick-users mailing list posting; tesseract-ocr mailing list posting
Thanks for your post ZakW, you pointed me to the right direction.
Anyhow i also needed to set '-depth 8'. Quality was not good enough for OCR, whatever I tried.
What worked for me is this solution:
ghostscript -o document.tiff -sDEVICE=tiffgray -r720x720 -g6120x7920 -sCompression=lzw document.pdf
tesseract document.tiff document -l deu
vim document.txt
This way I got perfect text with Umlauts in german.
Adjusting the conversion to the following line did help me.
convert -density 300 input.pdf -depth 8 -background white -alpha Off output.tiff
Note that the other answers did not work for me since they use the deprecated +matte flag instead of -alpha Off.
You can try using the command 'tiffinfo' provided by libtiff_tools to verify the TIFF format of your src image. A number of TIFF formats exist, with different values for Bits-per-pixel (bpp) and Samples-per-pixel (spp).
Error in pixReadFromTiffStream: spp not in set {1,3,4}
An 'spp' value of 2 is invalid for TIFF.
I solved the problem by saving directly to TIFF format from Gimp, instead of converting from .png to .tif using ImageMagick's 'convert'.
See also: TIFF format

Replace transparency in PNG image with white background

I have a PNG image with an alpha channel (i.e. transparency), and I need to create versions with the image layer composed onto a white background. I want to use a scriptable command using a CLI tool such as Image Magick to directly convert PNG to PNG losslessly.
An example of a non-working Image Magick command which results in an error is:
convert input.png -background white -flatten output.png
-background white -alpha remove -alpha off
Example:
convert image.png -background white -alpha remove -alpha off white.png
Feel free to replace white with any other color you want. Imagemagick documentation says this about the -alpha remove operation:
This operation is simple and fast, and does the job without needing
any extra memory use, or other side effects that may be associated
with alternative transparency removal techniques. It is thus the
preferred way of removing image transparency.
This works for me:
convert -flatten img1.png img1-white.png
Documentation references:
-flatten command-line option
-layers command-line option (-flatten is equivalent to -layers flatten)
Flattening image and applying background image is straight forward in ImageMagick
However, order of the commands is very important
To apply any background on a transparent image and flatten it, first apply the background than flatten it. The reverse doesn't work.
$ convert sourceimage.png -background BackgroundColor -flatten destinationimage.png
The only one that worked for me was a mix of all the answers:
convert in.png -background white -alpha remove -flatten -alpha off out.png
here's how to replace the same image in all folders in a directory with white instead of transparent:
mogrify -background white -flatten */*.png
Using -flatten made me completely mad because -flatten in combination with mogrify crop and resizing simply doesn't work. The official and for me only correct way is to "remove" the alpha channel.
-alpha remove -alpha off (not needed with JPG)
See documention: http://www.imagemagick.org/Usage/masking/#remove
The Alpha Remove section of the ImageMagick Usage Guide suggests using the -alpha remove option, e.g.:
convert in.png -background white -alpha remove out.png
...using the -background color of your choosing.
The guide states:
This operation is simple and fast, and does the job without needing any extra memory use, or other side effects that may be associated with alternative transparency removal techniques. It is thus the prefered way of removing image transparency.
It additionally adds the note:
Note that while transparency is 'removed' the alpha channel will remain turned on, but will now be fully-opaque. If you no longer need the alpha channel you can then use Alpha Off to disable it.
Thus, if you do not need the alpha channel you can make your output image size smaller by adding the -alpha off option, e.g:
convert in.png -background white -alpha remove -alpha off out.png
There are more details on other, often-used techniques for removing transparency described in the Removing Transparency from Images section.
Included in that section is mention of an important caveat to the usage of -flatten as a technique for removing transparency:
However this will not work with "mogrify" or with a sequence of multiple images, basically because the "-flatten" operator is really designed to merge multiple images into a single image.
So, if you are converting several images at once, e.g. generating thumbnails from a PDF file, -flatten will not do what you want (it will flatten all images for all pages into one image). On the other hand, using the -alpha remove technique will still produce multiple images, each one having transparency removed.
It appears that your command is correct so the problem might be due to missing support for PNG (). You can check with convert -list configure or just try the following:
sudo yum install libpng libpng-devel
This is not exactly the answer to your question, but I found your question while trying to figure out how to remove the alpha channel, so I decided to add this answer here:
If you want to remove alpha channel using imagemagick, you can use this command:
mogrify -alpha off ./*.png
Welp it looks like my decision to install "graphics magick" over "image magick" has some rough edges - when I reinstall genuine crufty old "image magick", then the above command works perfectly well.
edit, a long time later — One of these days I'll check to see if "graphics magick" has fixed this issue.
I needed either: both -alpha background and -flatten, or -fill.
I made a new PNG with a transparent background and a red dot in the middle.
convert image.png -background green -alpha off green.png failed: it produced an image with black background
convert image.png -background green -alpha background -flatten green.png produced an image with the correct green background.
Of course, with another file that I renamed image.png, it failed to do anything. For that file, I found that the color of the transparent pixels was "#d5d5d5" so I filled that color with green:
convert image.png -fill green -opaque "#d5d5d5" green.png replaced the transparent pixels with the correct green.
I saw this question and answers which really help me but then I was needed to do it for a lot of files, So in case you have multiple images (PNG images) in one folder and you want to do it for all:
find ./ -name "*.png" -exec convert {} -flatten {} \;
this creates an image just placing the 1st with transparency on top of the 2nd
composite -gravity center ImgWithTransp.png BackgroundSameSizeOfImg.png ResultImg.png
originally found the tip on this post
To actually remove the alpha channel from the file, use the alpha off option:
convert in.png -background white -alpha off out.png
Tried all, none worked. This one did:
convert input.png -channel rgba -alpha set \
-fill none -opaque white \
-fill white -opaque black \
-fill white -opaque none \
-alpha off output.png
It's -alpha off, NOT -alpha remove! iOS app store upload fails when there is an alpha channel in any icon!!
Here's how to do it:
mogrify -alpha off *.png
This does the job for me:
magick convert OLD.png -background white -alpha remove NEW.png
Here is a starter image with a transparent background in case it helps with testing:
Also, for one-offs on PC, you can always open the PNG file in Windows Paint and click Save. This will automatically turn the transparency to opaque white.