简体   繁体   中英

OpenCV:Detection colored spots

I would like to ask for a help with library OpenCV. I want to ask you if you know the best way how to detect a colored spot from picture. For example I need to create application which can calculate size of "dirty spot" on tshirt. Let's say that there is a brown tshirt and there is also a dirty spot made by katchup or by something else.

Could you recommend me algorithm or technics how to calculate it? Or some tutorial?

I wouldn't ask you for help but I am running out of time and perhaps you meet with that problem before.

Thank you very much.

In openCV samples codes (under cpp samples section), you can find a .cpp file named "bgfg_segm.cpp". Although that code is for motion tracking but i think that you can use to detect the spots also.

There by pressing the "spacebar key", you can start/stop updation of background. Once you have decided your background, then anything extra will be detected as a spot on it.

Strategy: Bring the cloth infront of webcam and once it is selected as a background, then press "spacebar key" to stop further changes in the background. Then, your program should be able to track any change in the color of your cloth.

The code is below:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{

    VideoCapture cap;
    bool update_bg_model = true;

    cap.open(0);

    namedWindow("image", CV_WINDOW_NORMAL);
    namedWindow("foreground mask", CV_WINDOW_NORMAL);
    namedWindow("foreground image", CV_WINDOW_NORMAL);
    namedWindow("mean background image", CV_WINDOW_NORMAL);

    // Declare "object " of class "BackgroundSubtractorMOG2"
    BackgroundSubtractorMOG2 bg_model;//(100, 3, 0.3, 5);

    Mat img, fgmask, fgimg;

    for(;;)
    {
        cap >> img;

        if( fgimg.empty() )
          fgimg.create(img.size(), img.type());

        //update the model
        bg_model(img, fgmask, update_bg_model ? -1 : 0); // "bg_model" is object of class "BackgroundSubtractorMOG2" as declared above.

        fgimg = Scalar::all(0);
        img.copyTo(fgimg, fgmask);

        Mat bgimg;
        bg_model.getBackgroundImage(bgimg);

        imshow("image", img);
        imshow("foreground mask", fgmask);
        imshow("foreground image", fgimg);
        if(!bgimg.empty())
          imshow("mean background image", bgimg );

        char k = (char)waitKey(30);
        if( k == 27 ) break;
        if( k == ' ' ) // Change the Background updation status by Spacebar key
        {
            update_bg_model = !update_bg_model; // initially "bool update_bg_model = true"
            if(update_bg_model)
                printf("Background update is on\n");
            else
                printf("Background update is off\n");
        }
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM