2017-02-21 261 views
-2

我正在研究Rubik的侧扫描仪以确定立方体的状态。我对计算机视觉非常陌生并且使用它,所以它一直是一个挑战。到目前为止,我所做的是我使用视频捕获,并在某些帧捕获该帧并将其保存以进行图像处理。这是它的样子。 enter image description here获取图像中像素的HSV值OpenCV

拍摄照片时,立方体每次都处于相同的位置,所以我不必担心定位贴纸。 我遇到的麻烦是在每个方块中获取一小部分像素来确定其HSV。

我知道HSV的范围大致

Red = Hue(0...9) AND Hue(151..180) 
Orange = Hue(10...15) 
Yellow = Hue(16..45) 
Green = Hue(46..100) 
Blue = Hue(101..150) 
White = Saturation(0..20) AND Value(230..255) 

所以之后我已经拍摄的图像,然后我加载它,分裂图像的HSV值,但不知道如何让某些像素坐标的形象。我该怎么做?

BufferedImage getOneFrame() { 
     currFrame++; 
     //At the 90th frame I capture that frame and save that frame 
     if (currFrame == 120) { 
      cap.read(mat2Img.mat); 

      mat2Img.getImage(mat2Img.mat); 

      Imgcodecs.imwrite("firstImage.png", mat2Img.mat); 

     } 
     cap.read(mat2Img.mat); 

     return mat2Img.getImage(mat2Img.mat); 
    } 

    public void splitChannels() { 

     IplImage firstShot = cvLoadImage("firstImage.png"); 
     //I split the channels so that I can determine the value of the pixel range 
     IplImage hsv = IplImage.create(firstShot.width(), firstShot.height(), firstShot.depth(), firstShot.nChannels()); 
     IplImage hue = IplImage.create(firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1); 
     IplImage sat = IplImage.create(firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1); 
     IplImage val = IplImage.create(firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1); 


     cvSplit(hsv, hue, sat, val, null); 
     //How do I get a small range of pixels of my images to determine get their HSV? 

    } 

回答

1

如果我理解你的问题,你知道你感兴趣的所有领域的坐标。将有关每个区域的信息保存到cvRect objects

您可以循环遍历矩形区域。做一个双重循环。外循环在rect.y开始并在rect.y + rect.height之前停止。在内部循环中,在x方向上做类似的事情。在循环内部,使用CV_IMAGE_ELEM macro来访问各个像素值并计算所需的任何内容。

虽然有一个建议:使用Mat而不是IplImage在使用OpenCV时有几个优点。我建议你开始使用'Mat',除非你有特殊的理由这样做。 Click to see the documentation并查看一个构造函数,其中一个参数为Mat和一个Rect。这个构造函数是你的好朋友 - 你可以创建一个新的Mat对象(不复制任何数据),它只包含矩形内的区域。

+0

没有具体的原因,为什么我使用'IplImage'我以前使用过'Mat',但是我找不到加载'Mat'图像的方法。并分割该图像的颜色通道。进入单独的HSV频道。 – cuber

+0

使用'imread'和'cvtColor'以及网上发现的众多例子。 :) 祝你好运! – Nejc

+0

我必须使用'cvtColor'我已经有了我想要的格式的图像。 – cuber