Input: face image
Problem: thresholded image before applying Canny to find contours but does not return face mask
Desired output if different face is input,it should generate a proper face mask(face area white and background white)
Tried with apple picture..works fine
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(){
Mat right=imread("front.jpg");
Mat img1;
cvtColor(right, img1, CV_RGB2GRAY);
threshold(img1,img1,160,255,cv::THRESH_BINARY);
Canny(img1, img1, 128, 350);
vector< vector<Point> > contours;
findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat mask = Mat::zeros(img1.rows, img1.cols, CV_8UC1);
drawContours(mask, contours, -1, Scalar(255), CV_FILLED);
normalize(mask.clone(), mask, 0.0, 255.0, CV_MINMAX, CV_8UC1);
imshow("original", right);
imshow("thresh",img1);
imshow("mask", mask);
waitKey(0);
return 0;
}
Here is the image that I used
Please ignore the first 3 comments below
threshold(img1,img1,160,255,cv::THRESH_BINARY_INV);
see the result here i.stack.imgur.com/VtB3Q.jpg , also you don't need to do canny before find contour. – Haris