Hmm, seems there is some problems with my code.
There are some more code
Code:
void RGB2HSV(uint8_t r, uint8_t g, uint8_t b, uint8_t* h, uint8_t* s, uint8_t* v){
unsigned __int8 rgbMin, rgbMax;
rgbMin = r < g ? (r < b ? r : b) : (g < b ? g : b);
rgbMax = r > g ? (r > b ? r : b) : (g > b ? g : b);
*v = rgbMax;
if (*v == 0) { *h = 0; *s = 0; return; }
*s = 255 * long(rgbMax - rgbMin) / (*v);
if (*s == 0) { *h = 0; return; }
if (rgbMax == r) *h = 0 + 43 * (g - b) / (rgbMax - rgbMin);
else if (rgbMax == g) *h = 85 + 43 * (b - r) / (rgbMax - rgbMin);
else *h = 171 + 43 * (r - g) / (rgbMax - rgbMin);
return;
}
void ShowLargeCallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (flags == cv::EVENT_FLAG_RBUTTON){
cv::Mat* scr = (cv::Mat*)userdata;
int type = scr->type();
int channels = scr->channels();
if (channels == 1){
std::cout << "Pos(" << x / 10 << ", " << y / 10 << ") Val( " << (int)scr->at<unsigned char>(y, x) << " )" << std::endl;
}
if (channels == 4){
int r = scr->at<cv::Vec4b>(y, x)[2];
int g = scr->at<cv::Vec4b>(y, x)[1];
int b = scr->at<cv::Vec4b>(y, x)[0];
unsigned __int8 h, s, v;
RGB2HSV(r, g, b, &h, &s, &v);
std::cout << "Pos( " << x / 10 << ", " << y / 10 << " ) RGB( " << r << ", " << g << ", " << b << " )";
std::cout << " HSV( " << (int)h << ", " << (int)s << ", " << (int)v << " )" << std::endl;
}
if (channels == 3){
int r = scr->at<cv::Vec3b>(y, x)[2];
int g = scr->at<cv::Vec3b>(y, x)[1];
int b = scr->at<cv::Vec3b>(y, x)[0];
unsigned __int8 h, s, v;
RGB2HSV(r, g, b, &h, &s, &v);
std::cout << "Pos( " << x / 10 << ", " << y / 10 << " ) RGB( " << r << ", " << g << ", " << b << " )";
std::cout << " HSV( " << (int)h << ", " << (int)s << ", " << (int)v << " )" << std::endl;
}
}
return;
}
void ShowLarge10x(cv::String winname, cv::Mat scr, bool wait_to_cklick){
int type = scr.type();
int channels = scr.channels();
cv::Mat Img(scr.rows * 10, scr.cols * 10, type);
if (channels == 1){
for (int row = 0; row < Img.rows; row++){
for (int col = 0; col < Img.cols; col++){
Img.at<unsigned char>(row, col) = scr.at<unsigned char>(row / 10, col / 10);
}
}
}
if (channels == 3){
for (int row = 0; row < Img.rows; row++){
for (int col = 0; col < Img.cols; col++){
Img.at<cv::Vec3b>(row, col) = scr.at<cv::Vec3b>(row / 10, col / 10);
}
}
}
if (channels == 4){
for (int row = 0; row < Img.rows; row++){
for (int col = 0; col < Img.cols; col++){
Img.at<cv::Vec4b>(row, col) = scr.at<cv::Vec4b>(row / 10, col / 10);
}
}
}
cv::imshow(winname, Img);
cv::setMouseCallback(winname, ShowLargeCallBackFunc, &Img);
if (wait_to_cklick) {
cv::waitKey(0);
cv::destroyWindow(winname);
}
return;
}
Show large shows image (1b, 3b, or 4b per pixel).
Also if you cklick right mouse button on any pixel it shows x,y RGB and HSV of that pixel.
Wery helpful, everybody must have this!