天嵌 ARM开发社区

 找回密码
 注册
查看: 3051|回复: 2

请教:读取USB数码显微镜像素分辨率过小

[复制链接]
hxf0223 发表于 2014-12-2 00:41:46 | 显示全部楼层 |阅读模式
从淘宝上买了个无驱USB数码显微镜(应该跟摄像头一样的用法),连接到windows 7上,分辨率大概为640x480,截图分辨率达到1280x1024。
从网上找了些代码,调试运行后,发现几个问题不知如何解决:
1,设备的像素分辨率过低,只有160x120,为win7上的1/4,不知道该如何设置提高视频分辨率?
2,转换YUVY为BITMAP后,像素分辨率也只有160x120,不知该如何提高截图像素分辨率?




附1:初始化代码:
  1. static int fd = -1;
  2. static struct v4l2_capability        cap;
  3. struct v4l2_fmtdesc                 fmtdesc;
  4. struct v4l2_format                         fmt, fmtack;
  5. struct v4l2_streamparm                 setfps;  
  6. struct v4l2_requestbuffers         req;
  7. struct v4l2_buffer                         buf;
  8. enum v4l2_buf_type                         type;
  9. unsigned char frame_buffer[IMAGEWIDTH * IMAGEHEIGHT * 3];


  10. struct buffer
  11. {
  12.         void* start;
  13.         unsigned int length;
  14. } *buffers;




  15. int enum_frame_intervals( int dev, __u32 pixfmt, __u32 width, __u32 height )  
  16. {
  17.         int ret;  
  18.         struct v4l2_frmivalenum fival;  

  19.         memset(&fival, 0, sizeof(fival));  
  20.         fival.index = 0;  
  21.         fival.pixel_format = pixfmt;  
  22.         fival.width = width;  
  23.         fival.height = height;

  24.         printf("\tTime interval between frame: ");  
  25.         while ( (ret = ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &fival)) == 0 )
  26.         {
  27.                 if (fival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
  28.                         printf("%u/%u, ",
  29.                                 fival.discrete.numerator, fival.discrete.denominator);  
  30.                 } else if (fival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {  
  31.                         printf("{min { %u/%u } .. max { %u/%u } }, ",  
  32.                                 fival.stepwise.min.numerator, fival.stepwise.min.numerator,  
  33.                                 fival.stepwise.max.denominator, fival.stepwise.max.denominator);  
  34.                         break;  
  35.                 } else if (fival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {  
  36.                         printf("{min { %u/%u } .. max { %u/%u } / "  
  37.                                 "stepsize { %u/%u } }, ",  
  38.                                 fival.stepwise.min.numerator, fival.stepwise.min.denominator,  
  39.                                 fival.stepwise.max.numerator, fival.stepwise.max.denominator,  
  40.                                 fival.stepwise.step.numerator, fival.stepwise.step.denominator);  
  41.                         break;  
  42.                 }
  43.                 fival.index++;
  44.         }

  45.         printf("\n");  
  46.         if ( ret != 0 && errno != EINVAL ) {  
  47.                 perror("ERROR enumerating frame intervals");  
  48.                 return errno;  
  49.         }  

  50.         return 0;  
  51. }  

  52. static int enum_frame_sizes(int dev, __u32 pixfmt)  
  53. {
  54.         int ret;
  55.         struct v4l2_frmsizeenum fsize;

  56.         memset(&fsize, 0, sizeof(fsize));
  57.         fsize.index = 0;
  58.         fsize.pixel_format = pixfmt;
  59.         while ( (ret = ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0 )
  60.         {
  61.                 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {  
  62.                         printf("{ discrete: width = %u, height = %u }\n",  
  63.                                 fsize.discrete.width, fsize.discrete.height);  
  64.                         ret = enum_frame_intervals(dev, pixfmt,  
  65.                                 fsize.discrete.width, fsize.discrete.height);  
  66.                         if (ret != 0)  
  67.                                 printf("  Unable to enumerate frame sizes.\n");  
  68.                 } else if (fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {  
  69.                         printf("{ continuous: min { width = %u, height = %u } .. "  
  70.                                 "max { width = %u, height = %u } }\n",  
  71.                                 fsize.stepwise.min_width, fsize.stepwise.min_height,  
  72.                                 fsize.stepwise.max_width, fsize.stepwise.max_height);  
  73.                         printf("  Refusing to enumerate frame intervals.\n");  
  74.                         break;  
  75.                 } else if (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE) {  
  76.                         printf("{ stepwise: min { width = %u, height = %u } .. "  
  77.                                 "max { width = %u, height = %u } / "  
  78.                                 "stepsize { width = %u, height = %u } }\n",  
  79.                                 fsize.stepwise.min_width, fsize.stepwise.min_height,  
  80.                                 fsize.stepwise.max_width, fsize.stepwise.max_height,  
  81.                                 fsize.stepwise.step_width, fsize.stepwise.step_height);  
  82.                         printf("  Refusing to enumerate frame intervals.\n");  
  83.                         break;
  84.                 }
  85.                 fsize.index++;
  86.         }

  87.         if ( ret != 0 && errno != EINVAL ) {
  88.                 perror("ERROR enumerating frame sizes");  
  89.                 return errno;
  90.         }

  91.         return 0;  
  92. }  

  93. static void video_list_formats( int dev )  
  94. {
  95.         struct v4l2_fmtdesc fmt;  
  96.         int ret;

  97.         memset(&fmt, 0, sizeof(fmt));
  98.         fmt.index = 0;
  99.         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  100.         while ( (ret = ioctl(dev, VIDIOC_ENUM_FMT, &fmt)) == 0 )
  101.         {
  102.                 printf("{ pixelformat = '%c%c%c%c', description = '%s' }\n",  
  103.                         fmt.pixelformat & 0xFF, (fmt.pixelformat >> 8) & 0xFF,  
  104.                         (fmt.pixelformat >> 16) & 0xFF, (fmt.pixelformat >> 24) & 0xFF,  
  105.                         fmt.description);

  106.                 ret = enum_frame_sizes(dev, fmt.pixelformat);  
  107.                 if ( ret != 0 )
  108.                         printf("Unable to enumerate frame sizes.\n");  

  109.                 fmt.index++;
  110.         }  

  111.         if ( errno != EINVAL ) {  
  112.                 perror("ERROR enumerating frame formats");  
  113.         }  
  114. }  



  115. int init_v4l2(void)
  116. {
  117.         //opendev
  118.         if ( (fd = open(FILE_VIDEO, O_RDWR)) == -1 )
  119.         {
  120.                 printf("Error opening V4L interface\n");
  121.                 return FALSE;
  122.         }

  123.         //query cap
  124.         if ( ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1 )
  125.         {
  126.                 printf("Error opening device %s: unable to query device.\n", FILE_VIDEO);
  127.                 return FALSE;
  128.         }
  129.         else
  130.         {
  131.              printf("driver:\t\t%s\n", cap.driver);
  132.              printf("card:\t\t%s\n", cap.card);
  133.              printf("bus_info:\t%s\n", cap.bus_info);
  134.              printf("version:\t%d\n", cap.version);
  135.              printf("capabilities:\t%x\n", cap.capabilities);

  136.              if ( (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == V4L2_CAP_VIDEO_CAPTURE )
  137.              {
  138.                         printf("Device %s: supports capture.\n", FILE_VIDEO);
  139.                 }

  140.                 if ( (cap.capabilities & V4L2_CAP_STREAMING) == V4L2_CAP_STREAMING )
  141.                 {
  142.                         printf("Device %s: supports streaming.\n", FILE_VIDEO);
  143.                 }
  144.         }

  145.         //emu all support fmt
  146. #if 0
  147.         fmtdesc.index = 0;
  148.         fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  149.         printf("Support format:\n");
  150.         while ( ioctl(fd,VIDIOC_ENUM_FMT, &fmtdesc) != -1 )
  151.         {
  152.                 printf("\t%d.%s\n", fmtdesc.index+1, fmtdesc.description);
  153.                 fmtdesc.index++;
  154.         }
  155. #else
  156.         video_list_formats(fd);
  157. #endif

  158.     //set fmt
  159.     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  160.         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  161.         fmt.fmt.pix.height = IMAGEHEIGHT;
  162.         fmt.fmt.pix.width = IMAGEWIDTH;
  163.         fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  164.        
  165.         if ( ioctl(fd, VIDIOC_S_FMT, &fmt) == -1 )
  166.         {
  167.                 printf("Unable to set format\n");
  168.                 return FALSE;
  169.         }
  170.        
  171.         if ( ioctl(fd, VIDIOC_G_FMT, &fmt) == -1 )
  172.         {
  173.                 printf("Unable to get format\n");
  174.                 return FALSE;
  175.         }
  176.        
  177.         {
  178.              printf("fmt.type:\t\t%d\n", fmt.type);
  179.              printf("pix.pixelformat:\t%c%c%c%c\n",
  180.                                                                 fmt.fmt.pix.pixelformat & 0xFF,
  181.                                                                 (fmt.fmt.pix.pixelformat >> 8) & 0xFF,(fmt.fmt.pix.pixelformat >> 16) & 0xFF,
  182.                                                                 (fmt.fmt.pix.pixelformat >> 24) & 0xFF);
  183.              printf("pix.height:\t\t%d\n", fmt.fmt.pix.height);
  184.              printf("pix.width:\t\t%d\n", fmt.fmt.pix.width);
  185.              printf("pix.field:\t\t%d\n", fmt.fmt.pix.field);
  186.         }

  187.         //set fps
  188.         setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  189.         setfps.parm.capture.timeperframe.numerator = 10;
  190.         setfps.parm.capture.timeperframe.denominator = 10;
  191.        
  192.         printf("init %s \t[OK]\n", FILE_VIDEO);

  193.         return TRUE;
  194. }
复制代码


附2:采集及转换BITMAP代码:
  1. int v4l2_grab(void)
  2. {
  3.         unsigned int n_buffers;
  4.        
  5.         //request for 4 buffers
  6.         req.count = 4;
  7.         req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  8.         req.memory = V4L2_MEMORY_MMAP;
  9.         if ( ioctl(fd, VIDIOC_REQBUFS, &req) == -1 ) {
  10.                 printf("request for buffers error\n");
  11.         }

  12.         //mmap for buffers
  13.         buffers = malloc(req.count * sizeof(*buffers));
  14.         if ( !buffers ) {
  15.                 printf ("Out of memory\n");
  16.                 return FALSE;
  17.         }
  18.        
  19.         for ( n_buffers = 0; n_buffers < req.count; n_buffers++ )
  20.         {
  21.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  22.                 buf.memory = V4L2_MEMORY_MMAP;
  23.                 buf.index = n_buffers;
  24.                
  25.                 //query buffers
  26.                 if ( ioctl(fd, VIDIOC_QUERYBUF, &buf) == -1 ) {
  27.                         printf("query buffer error\n");
  28.                         return FALSE;
  29.                 }

  30.                 buffers[n_buffers].length = buf.length;
  31.                
  32.                 // map
  33.                 buffers[n_buffers].start = mmap(NULL, buf.length, PROT_READ |PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
  34.                 if ( buffers[n_buffers].start == MAP_FAILED ) {
  35.                         printf("buffer map error\n");
  36.                         return FALSE;
  37.                 }
  38.         }
  39.                
  40.         //queue
  41.         for ( n_buffers = 0; n_buffers < req.count; n_buffers++ )
  42.         {
  43.                 buf.index = n_buffers;
  44.                 ioctl(fd, VIDIOC_QBUF, &buf);
  45.         }
  46.        
  47.         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  48.         ioctl(fd, VIDIOC_STREAMON, &type);
  49.        
  50.         ioctl(fd, VIDIOC_DQBUF, &buf);

  51.     printf("grab yuyv OK\n");
  52.         return TRUE;
  53. }


  54. int yuyv_2_rgb888(void)
  55. {
  56.         int i, j;
  57.     unsigned char y1, y2, u, v;
  58.     int r1, g1, b1, r2, g2, b2;
  59.     char* pointer;
  60.    
  61.         pointer = buffers[0].start;
  62.        
  63.     for ( i = 0; i < IMAGEHEIGHT; i++ )
  64.     {
  65.                 int wid = IMAGEWIDTH >> 1;
  66.             for ( j = 0; j < wid; j++ )
  67.             {
  68.                     y1 = *(pointer + (i*wid+j)*4);
  69.                     u  = *(pointer + (i*wid+j)*4 + 1);
  70.                     y2 = *(pointer + (i*wid+j)*4 + 2);
  71.                     v  = *(pointer + (i*wid+j)*4 + 3);
  72.                    
  73.                     r1 = y1 + 1.042 * (v-128);
  74.                     g1 = y1 - 0.34414 * (u-128) - 0.71414*(v-128);
  75.                     b1 = y1 + 1.772 * (u-128);
  76.                    
  77.                     r2 = y2 + 1.042*(v-128);
  78.                     g2 = y2 - 0.34414*(u-128) - 0.71414*(v-128);
  79.                     b2 = y2 + 1.772*(u-128);
  80.                    
  81.                     if( r1 > 255 ) r1 = 255;
  82.                     else if( r1 < 0 ) r1 = 0;
  83.                    
  84.                     if ( b1 > 255 ) b1 = 255;
  85.                     else if( b1 < 0 ) b1 = 0;       
  86.                    
  87.                     if( g1 > 255 ) g1 = 255;
  88.                     else if( g1 < 0 ) g1 = 0;       
  89.                            
  90.                     if ( r2 > 255 ) r2 = 255;
  91.                     else if( r2 < 0 ) r2 = 0;
  92.                    
  93.                     if( b2 > 255 ) b2 = 255;
  94.                     else if( b2 < 0 ) b2 = 0;       
  95.                    
  96.                     if ( g2 > 255 ) g2 = 255;
  97.                     else if( g2 < 0 ) g2 = 0;
  98.    
  99.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6) = (unsigned char)b1;
  100.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6 + 1) = (unsigned char)g1;
  101.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6 + 2) = (unsigned char)r1;
  102.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6 + 3) = (unsigned char)b2;
  103.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6 + 4) = (unsigned char)g2;
  104.                     *(frame_buffer + ((IMAGEHEIGHT-1-i)*wid+j)*6 + 5) = (unsigned char)r2;
  105.             }
  106.     }
  107.        
  108.     printf("change to RGB OK \n");
  109.         return 0;
  110. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
天嵌技术支持03 发表于 2014-12-2 11:01:13 | 显示全部楼层
        fmt.fmt.pix.height = IMAGEHEIGHT;
        fmt.fmt.pix.width = IMAGEWIDTH;
这里有没有正确设置
 楼主| hxf0223 发表于 2014-12-2 12:27:05 | 显示全部楼层
天嵌技术支持03 发表于 2014-12-2 11:01
fmt.fmt.pix.height = IMAGEHEIGHT;
        fmt.fmt.pix.width = IMAGEWIDTH;
这里有没有正确设 ...

谢谢大虾!
请教,这个是按照网上的帖子来的,如果不对该如何设置呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号 )

GMT+8, 2024-3-29 17:45 , Processed in 1.078125 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表