Ask Your Question

Revision history [back]

i need to convert from python to c++

Hi everyone, i have done a function in python to convert raw to jpeg with a lookuptable and denoising. The function in python works well but now i need the same function develoiped in c++; The raw is a 16bit 1channel grayscale 1533x1000

                     imageSize = (picture.height, picture.width)
                     if not os.path.exists(picture.output_file_path):
                         npimg = np.fromfile(picture.original_path, dtype=np.uint16)
                         npimg = npimg.reshape(imageSize)
                         npImage = bytescale(npimg)
                         min=np.min(npImage)        # result=144
                         max=np.max(npImage)        # result=216

                        LUT=np.zeros(256,dtype=np.uint8)
                        LUT[min:max+1]=np.linspace(start=0,stop=255,num=(max-min)+1,endpoint=True,dtype=np.uint8)

                        img = LUT[npImage]

                        img2 = img.copy()
                        cv2.fastNlMeansDenoising(img,img2,5,7,21)
                        Image.fromarray(img2).save(picture.output_file_path)

in c++

   Mat ycbcrFrame = Mat::zeros(height, width, CV_16UC1);
   Mat ycbcrFrame_final = Mat::zeros(height, width, CV_16U);
   Mat ycbcrFrame_final2 = Mat::zeros(height, width, CV_16U);
   Mat rgbFrame = Mat::zeros(height, width, CV_16UC1);

   Mat lookUpTable(1, 256, CV_16UC1);

   for( int i = 0; i < 256; ++i)
          lookUpTable.at<uchar>(0,i) = uchar(255-i);

    // data.raw is 544 * 288 * 2 = 313344 bytes long
   int rawSize = 2* width * height;

   FILE *file = fopen(rawFileName, "r");
   if (file == NULL)
   {
       cout << "Error opening " << rawFileName << endl;
       return;
   }

   fread(ycbcrFrame.data, sizeof(short), rawSize, file);
   fclose(file);

   //LUT(ycbcrFrame, lookUpTable, rgbFrame);

   ycbcrFrame.convertTo(ycbcrFrame_final, CV_8U,1/256.0);
   cv::fastNlMeansDenoising(ycbcrFrame_final,ycbcrFrame_final2,5,7,21);


   imwrite(output_file, ycbcrFrame_final2);

Sorry for the code in c++, is in development. I try to develop a linspace function in c++ but i don't now if is the problem.Now the c++ code works with the LUT call commented and give an image very similar to the python result but much more dark, i need to increase the white part. Someone can help me to create the right lookuptable like in python version?

Thanks Federico