0

I have a question dealing with the use of an image's width and widthstep.

I have the dimensions as well as the image data as a QByteArray. I also have the widthstep. My question is how to use the widthstep to properly populate the Ipl Image since my image's width is 4095, which is not an integer multiple of 8. I have successfully populated images with widths that are integer multiples of 8 but now I am stuck with this problem at hand. Any general code that uses widthstep to populate an IplImage would be welcomed and highly appreciated. :D

Here is the part of my switch case that works; the default integer multiple of 8 image width:

default:
      {
          IplImage* extracted_sar_image; // our image declaration
          CvSize size; // size type has width and height attributes
          size.height = sar_nrows_integer; // height of size
          size.width = sar_ncols_integer;

          extracted_sar_image = cvCreateImageHeader(size, IPL_DEPTH_8U, 1);
          extracted_sar_image->imageData = sar_image_data_2.data();         

          cvNamedWindow("Image", CV_WINDOW_NORMAL );
          cvShowImage("Image", extracted_sar_image); 
          cvMoveWindow("Image", 100, 100);
          cvSaveImage("C:/Users/z_slant/Desktop/generated_extracted_sar_image.bmp", extracted_sar_image); 
          // delay to observe the image that is being saved
          cvWaitKey(10000); 
          // deallocates the image data
          cvReleaseImage(&extracted_sar_image); 
          // closes image display window
          cvDestroyWindow("Image"); 

      break;
      }
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90

1 Answers1

1

you have to take into account the byte alignment from openCV structure.

1 line in openCV IplImage is widthStep bytes long

1 line in QbyteArray is width bytes long

thus, copying the data line by line should be ok.

qByteArray myArray;
IplImage* extracted_sar_image; // our image declaration
CvSize size; // size type has width and height attributes
size.height = sar_nrows_integer; // height of size
size.width = sar_ncols_integer;

extracted_sar_image = cvCreateImageHeader(size, IPL_DEPTH_8U, 1);

for (int row = 0 ; row < size.height ;row++)
{
    memcpy(&extracted_sar_image->imageData[row*extracted_sar_image->widthStep],&myArray.data()[row*size.width],size.width);
}
Trop_Wizz
  • 96
  • 3