基于鼠标点击的HSV分割

时间:2014-01-31 21:59:15

标签: opencv image-segmentation

我正在尝试做一些基本的图像分割。 HSV的阈值将根据单击鼠标的像素值确定。我无法让它发挥作用。这是我到目前为止写的:

#include <iostream>
#include "opencv2/opencv.hpp"
#include <cv.h>
#include "opencv/highgui.h"

using namespace std;
using namespace cv;

Mat edges,frame,bw,hsv,dst,src_gray,probabilistic_hough, HSV_image;
int H=110,S=40,V=140;
int border=80;
int linethreshold=180,linelengthslider=30,linegapslider=3, HT_threshold=10;
int min_threshold = 50;

void printHSVValues(int event, int x, int y, int, void* );
void changeborder( int, void* );

Scalar hsvlow(0,0,0),hsvhigh(180,255,255);

int c=0;

void printHSVValues(int event, int x, int y, int, void* );

int main ( int argc, char **argv )
{
    VideoCapture cap(1);

    namedWindow("edges",1);
    namedWindow("segmented",1);

    setMouseCallback( "edges", printHSVValues, 0 );

    while (c != 'q') {
        cap >> frame;
        imshow("edges",frame);
        c= (char)waitKey(100);
    }

    return 0;
}

void  printHSVValues(int event, int x, int y, int, void* ){

    int loop;
    float change;

    if( event == EVENT_LBUTTONDOWN ) {
        cvtColor(frame, HSV_image, CV_BGR2HSV);
        Vec3b p = HSV_image.at<float>(y,x);
        for (loop=0;loop<3;loop++) { 
            change=p[loop]*0.01; 
            hsvlow[loop]  = p[loop] - change; 
            hsvhigh[loop] = p[loop] + change;
        }
        inRange( HSV_image, hsvlow,hsvhigh,bw);
        imshow("segmented",bw);
    }
}

我搞砸了哪里?提前谢谢。

DY

1 个答案:

答案 0 :(得分:0)

您没有正确访问Mat值。在您正确分配类型Vec3b的变量时,图像有3个通道,但您从图像中检索float值。正确的方法是:

Vec3b p = HSV_image.at<Vec3b>(y,x);