Skip to main content

Posts

Showing posts with the label OpenCV

OpenCV : Face Detection

Why do you want to detect face? In general, face detection is used in many areas, such as auto focusing, game, etc. Today, we will make a program to synthesize images on the face by using face detection. First, we need a "haarcascade_frontalface_alt2.xml" file. This is used for face detect algorithm. We can take this file in following url.  https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt2.xml   And also we need images to synthesize on the face. I made a simple animal head for you.    Now, let's start make face detecting program. The full code is as follows. preprogress.h #ifndef DETACTFACE_PREPROGRASS_H #define DETACTFACE_PREPROGRASS_H #include <opencv2/opencv.hpp> using namespace cv ; using namespace std ; //load specific cascade file void load_cascade ( CascadeClassifier & cascade , string frame ){ string path = "./res/haarcascades/" ; string full_path = path + frame ; CV_...

OpenCV : Make sharpen image color

We sometimes take pictures where there is no light, so the pictures come out dark. This means it did not have a wide range of colors. For example, an image with a minimum value of 20 and a maximum value of 100 will be dark. So we need to wide the color range of the image 0 to 255. The full code is as follows. #include <iostream> #include <opencv2/opencv.hpp> using namespace cv ; using namespace std ; void meanVal ( Mat * inputArray ){ double minVal , maxVal ; minMaxIdx( * inputArray , & minVal , & maxVal ); //get min, max value of matrix double ratio = ( maxVal - minVal ) / 255 ; * inputArray = ( * inputArray - minVal ) / ratio ; // wide image range cout << minVal << "," << maxVal << endl; } int main () { Mat img = imread( "./res/woman.png" ); imshow( "origin" , img ); vector < Mat > bgr_img ; split( img , bgr_img ); // split matrix to 3...

OpenCV : Image synthesis (ROI, Mask, Binary)

Today we will make Image synthesis by using ROI, Masking, binarization. The full code is like that. #include <iostream> #include "opencv2/opencv.hpp" using namespace std ; using namespace cv ; int main () { Mat me = imread( "./res/pic/mh.jpg" ); Mat animal = imread( "./res/pic/animal_head.png" ); Mat animalGray ; Mat mask_ani ; Mat mask_ani_inv ; imshow( "me origin" , me ); imshow( "animal" , animal ); cvtColor( animal , animalGray , CV_BGR2GRAY); // color to black and white threshold( animalGray , mask_ani , 10 , 255 , 0 ); // make binary bitwise_not( mask_ani , mask_ani_inv ); imshow( "mask ani inv" , mask_ani_inv ); imshow( "mask ani" , mask_ani ); Mat imgRoi ; imgRoi = me ( Rect ( 50 , 50 , animal . cols , animal . rows ) ) ; imshow( "imgRoi" , imgRoi ); Mat me_back ; Mat ani_for ; bitwise_and( animal ...

OpenCV : Threshold (Binary Image)

1. What is binary Image? It is an image made up of two colors, black and white. It is useful to tracking shape or recognizing object. result binary images 2. How to make Binary Image? It can be made by using Threshold method and d epending on the type of flag, you can use various binarizations. double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type); Type : - THRESH_BINARY : if (src(x,y) > thresh) maxval else 0 - THRESH_BINARY_INV : if (src(x,y) > thresh) 0 else maxval - THRESH_TRUNC : if (src(x,y) > thresh) threshold else src(x,y) - THRESH_TOZERO : if (src(x,y) > thresh) src(x,y) else 0 - THRESH_TOZERO_INV : if (src(x,y) > thresh) 0 else src(x,y) Mat img = imread( "./res/text2.jpeg" ); Mat gray , binary , binary2 , binary3 , otsuBinary ; cvtColor( img , gray , CV_RGB2GRAY); // convert to gray color image // if scalar value is bigger then 128, the va...

OpenCV : Draw Contours

1. What is image contours? It is a line connecting pixels with the same value. In other words, it can be called a line connecting pixels of the same color. So it is useful to tracking shape or recognizing objects. contours example 2. How to draw image contours? First, you need convert to gray image and make binary image by using Threshold, because it helps image recognition. Mat img ; img = imread( "./res/mh.jpg" ); /* * convert to gray */ cvtColor( img , img , CV_RGB2GRAY); // convert image gray color   imshow( "gray" , img ); /* * binary */ // if scalar value is bigger then 128, the value changed to 255. else 0   threshold( img , img , 128 , 255 , CV_THRESH_BINARY); imshow( "binary" , img ); Next, you can find contours by using findContours method. And you can put the outputArray into drawContours method to draw contours. - void findContours ( InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierar...