Bitwise_and在opencv Java大小错误



我试图用java在opencv中做一个对象跟踪程序,当我试图将原始图片与阈值相结合时,我遇到了一个问题,我使用bitwise_and,我一直得到这个错误:

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........opencvmodulescoresrcarithm.cpp:1021: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function cv::binary_op
]
    at org.opencv.core.Core.bitwise_and_1(Native Method)
    at org.opencv.core.Core.bitwise_and(Core.java:1149)
    at opencv.Panel.main(Panel.java:151)

我不知道为什么,因为我认为矩阵有相同的大小,事实上,线程矩阵是基于原始矩阵创建的。

在这里我张贴我的代码:

package opencv;
 // Import the basic graphics classes.  
 // The problem here is that we read the image with OpenCV into a Mat object.  
 // But OpenCV for java doesn't have the method "imshow", so, we got to use  
 // java for that (drawImage) that uses Image or BufferedImage.  
 // So, how to go from one the other... Here is the way...  
 import java.awt.*;  
import java.awt.image.BufferedImage;  
import java.util.ArrayList;
import java.util.List; 
import javax.swing.*;  
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;  
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.VideoCapture;  
import org.opencv.imgproc.Imgproc;
 public class Panel extends JPanel{  
   private static final long serialVersionUID = 1L;  
   private BufferedImage image;  
   // Create a constructor method  
   public Panel(){  
     super();  
   }  
   private BufferedImage getimage(){  
     return image;  
   }  
   private void setimage(BufferedImage newimage){  
     image=newimage;  
     return;  
   }  
   /**  
    * Converts/writes a Mat into a BufferedImage.  
    *  
    * @param matrix Mat of type CV_8UC3 or CV_8UC1  
    * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY  
    */  
   public static BufferedImage matToBufferedImage(Mat matrix) {  
     int cols = matrix.cols();  
     int rows = matrix.rows();  
     int elemSize = (int)matrix.elemSize();  
     byte[] data = new byte[cols * rows * elemSize];  
     int type;  
     matrix.get(0, 0, data);  
     switch (matrix.channels()) {  
       case 1:  
         type = BufferedImage.TYPE_BYTE_GRAY;  
         break;  
       case 3:  
         type = BufferedImage.TYPE_3BYTE_BGR;  
         // bgr to rgb  
         byte b;  
         for(int i=0; i<data.length; i=i+3) {  
           b = data[i];  
           data[i] = data[i+2];  
           data[i+2] = b;  
         }  
         break;  
       default:  
         return null;  
     }  
     BufferedImage image2 = new BufferedImage(cols, rows, type);  
     image2.getRaster().setDataElements(0, 0, cols, rows, data);  
     return image2;  
   }    
   protected void paintComponent(Graphics g){  
        super.paintComponent(g);  
        //BufferedImage temp=new BufferedImage(640, 480, BufferedImage.TYPE_3BYTE_BGR);  
        BufferedImage temp=getimage();  
        //Graphics2D g2 = (Graphics2D)g;
        if( temp != null)
            g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);  
    }  

   public static void main(String arg[]) throws InterruptedException{  
    // Load the native library.  
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    //Frame Camara
    JFrame frame = new JFrame("Camera");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(400,400);  
    Panel panel1 = new Panel();  
    frame.setContentPane(panel1);       
    frame.setVisible(true); 
    //Frame HVS
    JFrame frame2 = new JFrame("HSV");  
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame2.setSize(400,400);  
    Panel panel2 = new Panel();  
    frame2.setContentPane(panel2);       
    frame2.setVisible(true); 
    //Frame Figure
    JFrame frame3 = new JFrame("Figure");  
    frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame3.setSize(400,400);  
    Panel panel3 = new Panel();  
    frame3.setContentPane(panel3);       
    frame3.setVisible(true); 

    //Definicion de Matrices
    Mat webcam_image=new Mat();  
    Mat hsv_image=new Mat();
    Mat thresholded=new Mat();   
    Mat figure=new Mat(); 


    //hvs colour range
    Scalar hsv_min = new Scalar(30, 100,100, 0);  
    Scalar hsv_max = new Scalar(70, 255,255, 0);    
    BufferedImage temp; 
    VideoCapture capture =new VideoCapture(0);  
    Thread.sleep(1000);
    if( capture.isOpened())  
     {  
      while( true )  
      {  
        capture.read(webcam_image);  
        if( !webcam_image.empty() )  
         {  
            hsv_image=bgr2hsv(webcam_image);
            Core.inRange(hsv_image, hsv_min, hsv_max, thresholded); 
            //TEST CODEE

            //System.out.println(webcam_image.size());
            //System.out.println(thresholded.size());

            Core.bitwise_and(webcam_image, thresholded, figure);  
            //////////////////////////////////////
            //Set Image 
            //Camera
           frame.setSize(webcam_image.width()+40,webcam_image.height()+60);  
           temp=matToBufferedImage(webcam_image);  
           panel1.setimage(temp);  
           panel1.repaint();  
           //HSV
           frame2.setSize(webcam_image.width()+40,webcam_image.height()+60);  
           temp=matToBufferedImage(thresholded); 
           panel2.setimage(temp);  
           panel2.repaint();
           //Figure
           frame3.setSize(webcam_image.width()+40,webcam_image.height()+60);  
           temp=matToBufferedImage(figure); 
           panel3.setimage(temp);  
           panel3.repaint();

         }  
         else  
         {  
           System.out.println(" --(!) No captured frame -- Break!");  
           break;  
         }  
        }  
       }  
       return;  
   }  
   private static void trackRobot() throws AWTException{       
   }

   private static Mat bgr2hsv(Mat bgr){
       Mat hsv = new Mat();
       Imgproc.cvtColor(bgr, hsv, Imgproc.COLOR_RGB2HSV);      
       return hsv;
   }
 }

Sorry for my english

Mat thresholded是CvType。CV_8UC1

正确的句法应该是

Core.bitwise_and(webcam_image, webcam_image, figure, thresholded);

或者您可以尝试将阈值转换为CvType。CV_8UC3

Imgproc.cvtColor(thresholded, thresholded, Imgproc.COLOR_GRAY2BGR, 3);

然后应用你的syntaxis

Core.bitwise_and(webcam_image, thresholded, figure);

相关内容

最新更新