Ask Your Question
0

Problem with HSV

asked 2016-12-28 23:15:51 -0500

Nuz gravatar image

updated 2016-12-29 02:18:00 -0500

LBerger gravatar image

Need some help, i am not able to find the desired contour

Input: image description

After applying binary filter: image description

After applying HSV filter: image description

HSV threshold code:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/ml.hpp>

using namespace std;
using namespace cv;


int main()
{
Size frameSize(500, 400);
Mat frame = imread("C:/Proj/HSVimage.jpg", CV_LOAD_IMAGE_COLOR);

Size srcSize(frame.size());

Mat hsv(srcSize, 3);
vector<Mat> hsvMask;
Mat hImg(srcSize, 1);
Mat sImg(srcSize, 1);


cvtColor(frame, hsv, CV_BGR2HSV);
//Vec3b hsv = hsv.at<Vec3b>(0, 0);
split(hsv, hsvMask);
hImg = hsvMask[0];
sImg = hsvMask[1];

imshow("hImg", hImg);
int h;
for (int i = 0; i < hImg.rows; i++)
{
for (int a = 0; a < hImg.cols; a++)
{
h = hImg.at<uchar>(i, a);

if (h >= 0 && h <= 25)
{
hImg.at<uchar>(i, a) = 255;
}
else
{
hImg.at<uchar>(i, a) = 0;
}


}
}

imshow("HSV Image", hImg);
imwrite("HSVThreshold.jpg",hImg);
waitKey(0);
return 0;
}

After thresholding the HSV image:

image description

Output: image description

However, the desired output should have been: image description

Need some help, this is my code for hsv:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main()
{
Mat image;
image = imread("C:/Proj/Denoised.jpg", CV_LOAD_IMAGE_COLOR);

if (!image.data)
{
    cout << "Could not open the denoised image" << std::endl;
    return -1;
}

// Create a new matrix to hold the hsv image
Mat HSV;

// convert RGB image to hsv image
cvtColor(image, HSV, CV_BGR2HSV);

namedWindow("Display RGB image", CV_WINDOW_AUTOSIZE);
imshow("Display RGB image", image);

//DISPLAY image
namedWindow("Result : HSV image", CV_WINDOW_AUTOSIZE);
imshow("Result : HSV image", HSV);

//save the HSV image
imwrite("C:/Proj/HSVimage.jpg", HSV);

waitKey(0);
return 0;
  }
edit retag flag offensive close merge delete

Comments

you need to threshold the HSV image to get only the skin regions and then find the contours

abhijith gravatar imageabhijith ( 2016-12-29 00:01:19 -0500 )edit

@abhijith Is the result after thresholding good?

Nuz gravatar imageNuz ( 2016-12-29 00:21:53 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-12-29 01:22:31 -0500

updated 2016-12-29 23:06:31 -0500

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int H_MIN = 0;
int H_MAX = 255;
int S_MIN = 0;
int S_MAX = 255;
int V_MIN = 0;
int V_MAX = 255;

void on_trackbar(int, void*)
 {

 }

void createTrackbars()
{
//create window for trackbars
namedWindow("Trackbars", 0);
//create memory to store trackbar name on window
char TrackbarName[50];
sprintf(TrackbarName, "H_MIN", H_MIN);
sprintf(TrackbarName, "H_MAX", H_MAX);
sprintf(TrackbarName, "S_MIN", S_MIN);
sprintf(TrackbarName, "S_MAX", S_MAX);
sprintf(TrackbarName, "V_MIN", V_MIN);
sprintf(TrackbarName, "V_MAX", V_MAX);
//create trackbars and insert them into window to change H,S,V values

createTrackbar("H_MIN", "Trackbars", &H_MIN, H_MAX, on_trackbar);
createTrackbar("H_MAX", "Trackbars", &H_MAX, H_MAX, on_trackbar);
createTrackbar("S_MIN", "Trackbars", &S_MIN, S_MAX, on_trackbar);
createTrackbar("S_MAX", "Trackbars", &S_MAX, S_MAX, on_trackbar);
createTrackbar("V_MIN", "Trackbars", &V_MIN, V_MAX, on_trackbar);
createTrackbar("V_MAX", "Trackbars", &V_MAX, V_MAX, on_trackbar);
}

int main()
{
 Mat image, HSV, threshold;
 vector< vector<Point> > contours;
 vector<Vec4i> hierarchy;

 createTrackbars();

 image = imread("thumb.jpg");
 imshow("Original_image",image);

 cvtColor(image, HSV, CV_BGR2HSV);
 imshow("HSV_image", HSV);
 for (;;)
 {
    inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);
    imshow("HSV_threshold", threshold);

    Mat result(threshold.size(), CV_8UC3, Scalar(0.0, 0.0, 0.0));
    findContours(threshold, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    drawContours(result, contours, -1, Scalar(255.0, 255.0, 255.0), 1, 8);
    imshow("contours_image", result);

    if (waitKey(30) >= 0)
        break;
 }

 return(0);
 }

image description

image description

image description

This is something like what you want, right?

It has a lot to do with the original image you've chosen as well. The illumination effects make it hard to get just the skin regions properly in your image, that's why your threshold image isn't too good.

edit: I guess this is the closest you'll get to what you want with your image. The two legs aren't detected separately because the original image isn't of the best quality. If this is the image you absolutely have to use then keep trying different thresholds and morphological operations and then find the contour with the largest area.

image description

edit flag offensive delete link more

Comments

Yes, i am expecting a similar result, but i have used a denoised image(median filter) and tranform it into hsv image. Part of the image is not visible after thresholding.Could it be because of the median filter, the HSV image is not that good? @ abhijith

Nuz gravatar imageNuz ( 2016-12-29 12:40:02 -0500 )edit

@Nuz, check the edit in my answer

abhijith gravatar imageabhijith ( 2016-12-29 22:58:33 -0500 )edit

Thank you @abhijith

Nuz gravatar imageNuz ( 2016-12-30 03:53:04 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-12-28 23:15:51 -0500

Seen: 1,366 times

Last updated: Dec 29 '16