1

im trying to do a object tracking program in opencv with java, and i have a problem when im tryng to combine the original pictrure with the thredholded one i use the bitwise_and and i keep getting this error:

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\arithm.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)

i dont know why because i think the matrix have the same size in fact the thredhoded matrix was created based on the original one.

Here i post my code:

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

3
  • Have you checked type of source matrix? Looks like it may be CV_32FC1 type, but output matrix of inRange is always CV_8UC1 type.
    – akarsakov
    May 26, 2015 at 6:46
  • both input images need to have same type. either use the version with mask or, more simple , use : webcam.copyTo(figure, thresholded);
    – berak
    May 26, 2015 at 8:03
  • thanky you i will try your solutions!!
    – Puzzero
    May 26, 2015 at 12:19

1 Answer 1

1

Mat thresholded is a CvType.CV_8UC1

The correct syntaxis should be

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

or you can try to to turn thresholded into a CvType.CV_8UC3

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

and then apply your syntaxis

Core.bitwise_and(webcam_image, thresholded, figure);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.