You are not accessing Mat-values correctly. The image has 3-channels as you correctly assigned the variable the type Vec3b
, yet you retrieve a float
value from the image. The correct way would be:
Vec3b p = HSV_image.at<Vec3b>(y,x);
سؤال
I am trying to do some basic image segmentation. The thereshold values for the HSV are to be determined from the pixel value where the mouse is clicked. I cannot get it to work. Here is what I wrote so far:
#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);
}
}
Where am I messing up? Thanks in advance.
DY.
المحلول
You are not accessing Mat-values correctly. The image has 3-channels as you correctly assigned the variable the type Vec3b
, yet you retrieve a float
value from the image. The correct way would be:
Vec3b p = HSV_image.at<Vec3b>(y,x);