Wednesday, May 4, 2011

Episode_6_Filtering_Images

FILTERING IMAGE
In the previous episode, you have learned how to load an image with OpenCV (Episode 2). For this episode, we will try how to editing of the image in the real time such as brightness, negatve, bluring, removing noise, etc. We will now move on to higher-level methods that treat the images as images, and not just as arrays of colored (or grayscale) values. When we say “image processing”, we mean just that: using higher-level operators that are defined on image structures in order to accomplish tasks whose meaning is naturally defi ned in the context of graphical, visual images.


Part A. Eroding the ImageI won't explain in detail what is erode? because you can learn it yourself. Here I will Erode the image 2 times;

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );
img2 = cvCloneImage(img);

cvErode(img2, img2, 0, 2);

cvShowImage("Image:",img);
cvShowImage("Eroded", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}



picture 0_eroding_images

Part B. Dilating the Image
Dilating the image is the opposite of Erode image.


#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );
img2 = cvCloneImage(img);

cvDilate(img2, img2, 0, 2);

cvShowImage("Image:",img);
cvShowImage("Dilate", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}


picture 1_dilating_images

Part C. BrightnessFor brightness, we use the cvAddS function. This function adds a scalar to each element of the matrix. There also exists a cvAdd function, but that function adds two matrices (not a matrix and a value).


#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );
img2 = cvCloneImage(img);

cvAddS(img2, cvScalar(50,50,50), img2);

cvShowImage("Image:",img);
cvShowImage("Brigth", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}


picture 2_brightness_image

Part D. Contrast
For brightness, you added values. For contrast, you multiply.
For adding scalars (values), you use cvAddS. For multiplying, you use cvScale

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );
img2 = cvCloneImage(img);

cvScale(img2, img2, 2);

cvShowImage("Image:",img);
cvShowImage("Contrast", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}


picture 3_contrast_image

Part E. NegativeInverting the image such as giving a NOT logic operation on each element matrix.
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );
img2 = cvCloneImage(img);

cvNot(img2, img2);

cvShowImage("Image:",img);
cvShowImage("Negative", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}


picture 4_negative_images

Part F. GrayColorFor further image processing, is generally used to change the image in the form of a binary image so its can facilitate the morpological operation such as threshold, laplace, Canny, and others.

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage *img = cvLoadImage("Step_forward.jpg");
IplImage *img2= cvCreateImage( cvGetSize(img), 8,1 );

cvCvtColor(img,img2,CV_BGR2GRAY);

cvShowImage("Image:",img);
cvShowImage("Gray", img2);

cvWaitKey();
cvReleaseImage(&img);
cvReleaseImage(&img2);

return 0;
}
picture 5_graycolor

Okay these are just some of the existing filtering process in OpenCV. I will continue it at any time.

Referensi for Beginner

2 comments:

anonimo said...

You know how they can do this example in a "Windows Form" I try, and image in "gray scale" appears 3 times! : S
Try changing the PixelFormat but did not work.
Know how to do it?

Tomás

Unknown said...

Sory I don't have a solution for your problem.
Cause grayscale image in 8 bit color image, but picturebox must be in 24 bit color(RGB 3-channel) Pixelformat, so we must convert it before.

frame= cvQueryFrame(capture); cvCvtColor(frame,img,CV_BGR2GRAY);
cvSaveImage("tes.jpg",img);
img2 = cvLoadImage("tes.jpg",1);
pictureBox1->Image = gcnew //replacement of cvShowImage
System::Drawing::Bitmap(img2->width,img2->height,img2->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) img2->imageData);
pictureBox1->Refresh();

maybe you can use this to convert it, but I think it's not good idea for continuously. cause it will make an unhandled exception(over memory)

Post a Comment