Home > Enterprise >  How to change this C (OpenCV) code so it takes an image instead of a video?
How to change this C (OpenCV) code so it takes an image instead of a video?

Time:09-22

I am new to C and trying to convert this code so it can take an image instead of a video. This is for face detection with OpenCV. I can figure out that to Mat, I need to pass an image instead of video, just not sure how exactly to do that. Any help will be appreciated.

Thanks.

int main()
{
VideoCapture capture;
//open capture object at location zero (default location for webcam)

capture.open(0);

//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH,320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);

Mat cameraFeed;

SkinDetector mySkinDetector;

Mat skinMat;

//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
while(1){

//store image to matrix
capture.read(cameraFeed);

//show the current image
imshow("Original Image",cameraFeed);

skinMat= mySkinDetector.getSkin(cameraFeed);

imshow("Skin Image",skinMat);

waitKey(30);
}
return 0;
}

CodePudding user response:

You have to change the whole code. In this code, you are using continuous images from video using a while loop. Use the single image as

Mat cameraFeed = imread("/path/to/image.jpg");
imshow("Original Image",cameraFeed);

Mat skinMat= mySkinDetector.getSkin(cameraFeed);
imshow("Skin Image",skinMat);

waitKey(30);

CodePudding user response:

I'm not sure what you mean by 'pass an image'. If you're not reading from a video source and want to read from a file on disk then remove the following code:

VideoCapture capture;
//open capture object at location zero (default location for webcam)

capture.open(0);

//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH,320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);

And change Mat cameraFeed to:

Mat cameraFeed = imread("/path/to/image.jpg");

Or you could just create a function and pass your image to that.

void detectSkin(Mat img)
{
    SkinDetector mySkinDetector;
    Mat skinMat;

    //show the current image
    imshow("Original Image",img);

    skinMat= mySkinDetector.getSkin(img);

    imshow("Skin Image",skinMat);

    // Changed to 3000 milliseconds so you'll have time to see it.
    waitKey(3000);
}

int main()
{
    Mat img = imread("myimage.jpg");
    detectSkin(img);

    return 0;
}
  • Related