2013-02-17 101 views
5

का उपयोग करके निर्देशांक और पिक्सेल मान प्रिंट करें यह कोड है जिसे मैंने कोशिश की, केवल समन्वय मान मुद्रित हैं लेकिन पिक्सेल मान नहीं।माउस कॉलबैक

#include <iostream> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 

void onMouse(int event, int x, int y, int, void*); 
using namespace cv; 

Mat img = cv::imread("b.jpg", 0); // force grayscale 
Mat thresh=Mat::zeros(img.size(),CV_8UC1); 

int main(int argc, char **argv) 
{ 

    if(!img.data) { 
     std::cout << "File not found" << std::endl; 
     return -1; 
    } 

    threshold(img,binary,50,255,THRESH_TOZERO); 

    namedWindow("thresh"); 
    setMouseCallback("thresh", onMouse, 0); 

    imshow("thresh",thresh); 
} 

void onMouse(int event, int x, int y, int, void*) 
{ 
    if(event != CV_EVENT_LBUTTONDOWN) 
      return; 

    Point pt = Point(x,y); 
    std::cout<<"x="<<pt.x<<"\t y="<<pt.y<<"\t value="<<thresh.at<uchar>(x,y)<<"\n"; 

} 

मैं के रूप में उत्पादन मिला: -

screenshot

समन्वय मूल्यों मुद्रित कर रहे हैं लेकिन पिक्सेल मान ठीक से मुद्रित नहीं कर रहे हैं। मैंने क्या गलती की है ??

उत्तर

5

cout प्रिंट्स uchar के पात्रों के रूप में प्रिंट करता है, जैसा कि आप देखते हैं।

सिर्फ एक कलाकारों के साथ उन्हें लपेटो मुद्रण के लिए int करने के लिए:

cout << int(thresh.at<uchar>(y,x)) 

also note, that it's at<uchar>(y,x), not x,y 
+0

आप @berak धन्यवाद। एक और qn: .at (वाई, एक्स) में, कौन सा पंक्ति संख्या और स्तंभ संख्या का प्रतिनिधित्व करता है ?? –

+0

पर (row_no, column_no) 'मूल रूप से: y = row_no, x = column_no' – berak