c++ - Accidental quantization in BGR to HSV conversion -


i trying convert bgr image hsv. when write out h channel of conversion, has strange blocky structure i'm guessing means got accidentally quantized along way. tried converting bgr unsigned char image float first, result same. here code:

// stl #include <iostream>  // opencv #include <opencv2/opencv.hpp>  void float(const std::string& inputfilename) {     cv::mat image = cv::imread(inputfilename, cv_load_image_color); // loads bgr      cv::mat floatimage;     image.convertto(floatimage, cv_32fc3);      cv::mat hsvimage;     cv::cvtcolor(floatimage, hsvimage, cv_bgr2hsv);      std::vector<cv::mat> hsvchannels;     cv::split(hsvimage, hsvchannels);      cv::imwrite("h_float.png", hsvchannels[0]);  }  void original(const std::string& inputfilename) {     cv::mat image = cv::imread(inputfilename, cv_load_image_color); // loads bgr      cv::mat hsvimage;     cv::cvtcolor(image, hsvimage, cv_bgr2hsv);      std::vector<cv::mat> hsvchannels;     cv::split(hsvimage, hsvchannels);      cv::imwrite("h_original.png", hsvchannels[0]);  }  int main(int argc, char* argv[]) {     std::string inputfilename = argv[1];      original(inputfilename);     float(inputfilename);      return exit_success; } 

and here input: enter image description here

and output (h_original.png): enter image description here

and output (h_float.png): enter image description here

any suggestion i'm doing wrong here?

you doing in right way. tried image on machine , got same results. think result due low color range of image. result of 3 channels of hsv. h , s suffer problem. however, v smooth because light condition not color.

h-channel

s-channel

v-channel

edit:

to make point clearer, see image , output using same code:

original rgb

h-channel

s-channel

v-channel

another edit (black problem):

in hsv: if v=0 black whatever rest s , h are. check if color black have check v component , there no meaning if checking other.

math proof:

c = v × s x = c × (1 - |(h / 60ยบ) mod 2 - 1|) m = v - c 

hsv-rgb

it clear when v=0 , whatever h,s are, have: c=0 , x=0 leads r,g,b=(0,0,0)


Comments