Image Processing

Today I fixed a memory bug in one of my programs. Albeit I always release memory cvReleaseImage after allocating it with cvCreateImage, memory consumption was enormous. Commenting out some of the source code quickly showed the responsible code fragments. Using cvCreateImage and afterwards cvCloneImage made it impossible to release the initially allocated memory. Therefore after some iterations (depending on your main memory) this program will crash. To fix this issue simply replace the cvCloneImage with cvCopy.

Bad code:

DO
 img2 = cvCreateImage(cvSize(img->width, img->height), img->depth, img->nChannels);
[...]
 img = cvCloneImage(img2);
 cvReleaseImage(&img2);
LOOP

Good code:

DO
 img2 = cvCreateImage(cvSize(img->width, img->height), img->depth, img->nChannels);
[...]
 //img = cvCloneImage(img2);
 cvCopy(img2,img,NULL);
 cvReleaseImage(&img2);
LOOP

Last Updated (Thursday, 08 July 2010 21:54)

 

Sometimes single color-channels are needed for further processing and therefore we have to grab a graphic tool of our choice to accomplish that task. If you have a stack of images it can get very tedious. So why not use a handy script? I've written a short bash script that extracts red, green and blue channel from all images in the current directory storing each channel with postfixes _red|_green|_blue. Again you can change the output format at line 16, 18 and 20 to your desired format. If your input images are not PNG files then you have to change the input format at line 7 (e.g. .png -> .jpg).

#/bin/bash
# RGB splitter
#

#set debug

find . -type f | /bin/grep .png$ |while read file
do
 filenoext=${file%%.png}
 filenameout=$filenoext.png #change filetype here
 echo "Processing $file"
 echo "Output R=$filenoext""_red.png"
 echo "Output G=$filenoext""_green.png"
 echo "Output B=$filenoext""_blue.png"
 echo "Processing R"
convert "$file" -channel R -separate "$filenoext"_red.png
 echo "Processing G"
convert "$file" -channel G -separate "$filenoext"_green.png
 echo "Processing B"
convert "$file" -channel B -separate "$filenoext"_blue.png
done

Last Updated (Wednesday, 07 October 2009 22:20)

 

ImageMagick provides a lot of useful tools for image processing. Today I want to introduce convert which is part of the ImageMagick bundle. Often I get images in a format I cannot process or which takes a huge amount of space on my hard disk. Using convert it's quite easy to store them e.g. in PNG. Imagine a set of TIF images that should be converted to PNG. The script below will do this task within seconds and without any further human interaction.

 

#/bin/bash
# tif2png

#set debug

find . -type f | /bin/grep .tif$ |while read file
do
 filenoext=${file%%.tif}
 filenameout=$filenoext.png #change filetype here
 echo "Processing $file -> $filenameout"
 convert  "$file" "$filenameout"
done 

 

Substitute tif in line 8 with any other image input-format and png in line 9 with your desired output format.

Last Updated (Wednesday, 07 October 2009 21:16)

 
Sponsored links: