Results 1 to 3 of 3

Thread: Help needed handling image data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Help needed handling image data

    Hi

    I'm trying to use code for an encoder from a project I found on CodeGuru, but I'm not sure how to use it. This is the first method I need to use:

    Qt Code:
    1. //
    2. // Convert from RGB24 to YUV420
    3. //
    4. int ConvertRGB2YUV(int w,int h,unsigned char *bmp,unsigned int *yuv)
    5. {
    6.  
    7. unsigned int *u,*v,*y,*uu,*vv;
    8. unsigned int *pu1,*pu2,*pu3,*pu4;
    9. unsigned int *pv1,*pv2,*pv3,*pv4;
    10. unsigned char *r,*g,*b;
    11. int i,j;
    12.  
    13. uu=new unsigned int[w*h];
    14. vv=new unsigned int[w*h];
    15.  
    16. if(uu==NULL || vv==NULL)
    17. return 0;
    18.  
    19. y=yuv;
    20. u=uu;
    21. v=vv;
    22.  
    23. // Get r,g,b pointers from bmp image data....
    24. r=bmp;
    25. g=bmp+1;
    26. b=bmp+2;
    27.  
    28.  
    29. //Get YUV values for rgb values...
    30.  
    31. for(i=0;i<h;i++)
    32. {
    33.  
    34. for(j=0;j<w;j++)
    35. {
    36. *y++=( RGB2YUV_YR[*r] +RGB2YUV_YG[*g]+RGB2YUV_YB[*b]+1048576)>>16;
    37. *u++=(-RGB2YUV_UR[*r] -RGB2YUV_UG[*g]+RGB2YUV_UBVR[*b]+8388608)>>16;
    38. *v++=( RGB2YUV_UBVR[*r]-RGB2YUV_VG[*g]-RGB2YUV_VB[*b]+8388608)>>16;
    39.  
    40. r+=3;
    41. g+=3;
    42. b+=3;
    43. }
    44.  
    45. }
    46.  
    47.  
    48.  
    49. // Now sample the U & V to obtain YUV 4:2:0 format
    50.  
    51. // Sampling mechanism...
    52. /* @ -> Y
    53. # -> U or V
    54.  
    55. @ @ @ @
    56. # #
    57. @ @ @ @
    58.  
    59. @ @ @ @
    60. # #
    61. @ @ @ @
    62.  
    63. */
    64.  
    65. // Get the right pointers...
    66. u=yuv+w*h;
    67. v=u+(w*h)/4;
    68.  
    69. // For U
    70. pu1=uu;
    71. pu2=pu1+1;
    72. pu3=pu1+w;
    73. pu4=pu3+1;
    74.  
    75. // For V
    76. pv1=vv;
    77. pv2=pv1+1;
    78. pv3=pv1+w;
    79. pv4=pv3+1;
    80.  
    81. // Do sampling....
    82. for(i=0;i<h;i+=2)
    83. {
    84.  
    85. for(j=0;j<w;j+=2)
    86. {
    87. *u++=(*pu1+*pu2+*pu3+*pu4)>>2;
    88. *v++=(*pv1+*pv2+*pv3+*pv4)>>2;
    89.  
    90. pu1+=2;
    91. pu2+=2;
    92. pu3+=2;
    93. pu4+=2;
    94.  
    95. pv1+=2;
    96. pv2+=2;
    97. pv3+=2;
    98. pv4+=2;
    99. }
    100.  
    101. pu1+=w;
    102. pu2+=w;
    103. pu3+=w;
    104. pu4+=w;
    105.  
    106. pv1+=w;
    107. pv2+=w;
    108. pv3+=w;
    109. pv4+=w;
    110.  
    111. }
    112.  
    113.  
    114. delete uu;
    115. delete vv;
    116.  
    117. return 1;
    118. }
    To copy to clipboard, switch view to plain text mode 


    ------------------------------------------------------------------------------------------


    Now, my input will be bmp, which is an unsigned char. The image I have is a IplImage, which has this structure:

    Qt Code:
    1. // IPL image header
    2. typedef struct _IplImage
    3. {
    4. int nSize; /* sizeof(IplImage) */
    5. int ID; /* version (=0)*/
    6. int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
    7. int alphaChannel; /* ignored by OpenCV */
    8. int depth; /* pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
    9. IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported */
    10. char colorModel[4]; /* ignored by OpenCV */
    11. char channelSeq[4]; /* ditto */
    12. int dataOrder; /* 0 - interleaved color channels, 1 - separate color channels.
    13. cvCreateImage can only create interleaved images */
    14. int origin; /* 0 - top-left origin,
    15. 1 - bottom-left origin (Windows bitmaps style) */
    16. int align; /* Alignment of image rows (4 or 8).
    17. OpenCV ignores it and uses widthStep instead */
    18. int width; /* image width in pixels */
    19. int height; /* image height in pixels */
    20. struct _IplROI *roi;/* image ROI. when it is not NULL, this specifies image region to process */
    21. struct _IplImage *maskROI; /* must be NULL in OpenCV */
    22. void *imageId; /* ditto */
    23. struct _IplTileInfo *tileInfo; /* ditto */
    24. int imageSize; /* image data size in bytes
    25. (=image->height*image->widthStep
    26. in case of interleaved data)*/
    27. char *imageData; /* pointer to aligned image data */
    28. int widthStep; /* size of aligned image row in bytes */
    29. int BorderMode[4]; /* border completion mode, ignored by OpenCV */
    30. int BorderConst[4]; /* ditto */
    31. char *imageDataOrigin; /* pointer to a very origin of image data
    32. (not necessarily aligned) -
    33. it is needed for correct image deallocation */
    34. }
    To copy to clipboard, switch view to plain text mode 



    ----------------------------------------------------------------------------------------

    On the other side, I have an IplImage to DIB conversor, like this:

    Qt Code:
    1. HBITMAP IplImageWidget::IplImage2DIB(const IplImage* Image){
    2. int bpp = Image->nChannels * 8;
    3. assert(Image->width >= 0 && Image->height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
    4. CvMat dst;
    5. void* dst_ptr = 0;
    6. HBITMAP hbmp = NULL;
    7. unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
    8. BITMAPINFO* bmi = (BITMAPINFO*)buffer;
    9. BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
    10.  
    11. ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
    12. bmih->biSize = sizeof(BITMAPINFOHEADER);
    13. bmih->biWidth = Image->width;
    14. bmih->biHeight = Image->origin ? abs(Image->height) : -abs(Image->height);
    15. bmih->biPlanes = 1;
    16. bmih->biBitCount = bpp;
    17. bmih->biCompression = BI_RGB;
    18.  
    19. if (bpp == 8) {
    20. RGBQUAD* palette = bmi->bmiColors;
    21. int i;
    22. for (i = 0; i < 256; i++) {
    23. palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = (BYTE)i;
    24. palette[i].rgbReserved = 0;
    25. }
    26. }
    27.  
    28. hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0, 0);
    29. cvInitMatHeader(&dst, Image->height, Image->width, CV_8UC3,
    30. dst_ptr, (Image->width * Image->nChannels + 3) & -4);
    31. cvConvertImage(Image, &dst, Image->origin ? CV_CVTIMG_FLIP : 0);
    32.  
    33. return hbmp;
    34. }
    To copy to clipboard, switch view to plain text mode 


    ------------------------------------------------------------------------------------


    So, coming from the IplImage or the DIB, what can I use so that I get something that works with ConvertRGB2YUV?


    Thanks
    Last edited by wysota; 11th May 2007 at 09:23. Reason: missing [code] tags

Similar Threads

  1. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  2. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  3. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. reading in image from 24bit char*
    By cbeall1 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2006, 00:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.