Ask Your Question

erohan's profile - activity

2020-10-25 07:19:19 -0600 received badge  Student (source)
2020-10-25 07:18:13 -0600 received badge  Notable Question (source)
2017-03-16 09:45:22 -0600 received badge  Popular Question (source)
2016-02-24 07:10:06 -0600 commented answer IP camera video corruption - solutions?

This approach is working also at Java side. OpenCV 3.x release had fixed issues at ffmpeg side. So as sturkmen said renaming the dll through your 2.x release name and using it with threads will solve the problems. Through this you will not need to integrate libvlc or any other custom solution or external library.

The approach is like that :

0- Rename the 3.x ffmpeg dll to 2.X and replace it on OpenCV base setup folder.

1- In a class use a method only reading next frame. Simply no additional process here :

Mat image;
if (videoSignalOkay) {
caprure.read(image);
}

2- In a render thread get the bufferedimage from Mat

while (streaming) { 
Thread.sleep(5); // Give a breath for CPU
streamer.read();
}

3- Updater thread will get the same reference from class of

2016-01-28 13:26:56 -0600 received badge  Teacher (source)
2015-12-30 08:07:33 -0600 answered a question Raspberry Pi with OpenCV - Use Raspbian or Ubuntu ?

I have a BananaPi and I installed Lubuntu on it from lemaker.org.

You can use Python and Java very clean on Lubuntu. I tried some sorting algorithms with Java on it. So, i prefer ubuntu based distributions because of its strong community support also.

And you may lookup SimpleCV also as an alternative if you want to go with Python especially. http://simplecv.org/

2015-12-30 07:37:56 -0600 answered a question Motion estimation between 2 frames

I understood that you want to indicate the areas on frame which have motion. You may use motion history approach or contour approach for this work.

Here is a motion history approach used sample method (optimized through forking the OpenCV sample - update_mhi) written with Java. You may call this method in a loop through passing the retrieved video frame's Mat reference to img parameter of method. // Fields... Mat motion, mhi, orient, mask, segmask;

private void update_mhi(Mat img, Mat dst, int diff_threshold) {
    if (videoSignalOkay) {
        double timestamp = (System.nanoTime() - startTime) / 1e9;
        int idx1 = last, idx2;
        Mat silh = Mat.zeros(size, CvType.CV_32FC1);
        cvtColor(img, buf[last], COLOR_BGR2GRAY);
        double angle, count;

        idx2 = (last + 1) % Constants.fps; // index of (last - (N-1))th frame
        last = idx2;

        silh = buf[idx2];
        if (silh == null || silh.empty()) {
            silh = Mat.zeros(size, CvType.CV_32FC1);
        }
        absdiff(buf[idx1], buf[idx2], silh);

        threshold(silh, silh, diff_threshold, 1, THRESH_BINARY);

        updateMotionHistory(silh, mhi, timestamp, Constants.mhiDuration);

        mhi.convertTo(mask, mask.type(), 255.0 / Constants.mhiDuration, (Constants.mhiDuration - timestamp) * 255.0 / (Constants.mhiDuration));
        dst.setTo(new Scalar(0));
        List<Mat> list = new ArrayList<Mat>(3);
        list.add(mask);
        list.add(Mat.zeros(mask.size(), mask.type()));
        list.add(Mat.zeros(mask.size(), mask.type()));

        merge(list, dst);

        calcMotionGradient(mhi, mask, orient, Constants.maxTimeDelta, Constants.minTimeDelta, 3);

        MatOfRect roi = new MatOfRect();
        segmentMotion(mhi, segmask, roi, timestamp, Constants.maxTimeDelta);

        int total = roi.toArray().length;
        Rect[] rois = roi.toArray();
        Rect comp_rect;
        Scalar color;

        for (int i = -1; i < total; i++) {
            if (i < 0) {
                comp_rect = new Rect(0, 0, videoWidth, videoHeight);
                color = new Scalar(255, 255, 255);
                magnitude = 100;
            } else {
                comp_rect = rois[i];
                if (comp_rect.width >= videoWidth/2 || comp_rect.height >= videoHeight/2 || 
                    comp_rect.width < Constants.recfactorx || comp_rect.height < Constants.recfactory || 
                    comp_rect.width + comp_rect.height < (Constants.recfactorx*Constants.recfactory)) // reject very small things
                    continue;
                color = new Scalar(0, 0, 255);
                magnitude = 30;
            }

            Mat silhROI = silh.submat(comp_rect);
            Mat mhiROI = mhi.submat(comp_rect);
            Mat orientROI = orient.submat(comp_rect);
            Mat maskROI = mask.submat(comp_rect);

            angle = calcGlobalOrientation(orientROI, maskROI, mhiROI, timestamp, Constants.mhiDuration);
            angle = 360.0 - angle;
            count = Core.norm(silhROI, NORM_L1);

            silhROI.release();
            mhiROI.release();
            orientROI.release();
            maskROI.release();
            if (count < comp_rect.height * comp_rect.width * Constants.pixelFactor ||
                comp_rect.width == videoWidth || comp_rect.height == videoHeight) {
                continue;
            } else {
                isAnyTrack = true;
            }
            Point center = new Point((comp_rect.x + comp_rect.width / 2), (comp_rect.y + comp_rect.height / 2));
            // Optimizer part! Compare the tracked thing in previous list to control empty area movement detection...
            /*if (isAnyTrack) {
                TrackingPojo trackPojo = new TrackingPojo(comp_rect, center, null); 
                if (notNeededTracking(trackPojo)) {
                    isAnyTrack = false;
                    continue;
                } else {
                    trackList.add(trackPojo);
                    // Show the warning icon...
                    ControlManager.getTheInstance().toggleAlert(true);
                }
            }*/
            circle(img, center, (int) Math.round(magnitude * 1.2), color, 3, LINE_AA, 0);
            Point thePoint = new Point(
                    Math.round(center.x - magnitude * Math.sin(angle * Math.PI / 180)),
                    Math.round(center.y + magnitude * Math.cos(angle * Math.PI / 180)));
            Point thePoint2 = new Point(thePoint.x, thePoint.y - (comp_rect.height / 2 + 10));
            Point thePoint3 = new Point(thePoint2.x, thePoint2.y - 15);
            Core.putText(img, "(" + center.x + ", " + center.y + ")", thePoint, 16, 0.50, new Scalar(255, 0, 0 ...
(more)
2015-11-23 05:17:41 -0600 commented question After 2.4.12.3 Windows Build from source - Java Unsatisfied Link Error

I compared the jni_part.cpp of 2.4.11.0 with 2.4.12.3 and saw that first load of library mechanism changed as below :

JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM* vm, void* )
{
    JNIEnv* env;
    if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
        return -1;

// Change starts here...
    bool init = true;
#ifdef HAVE_OPENCV_NONFREE
    init &= cv::initModule_nonfree();
#endif
#ifdef HAVE_OPENCV_FEATURES2D
    init &= cv::initModule_features2d();
#endif
#ifdef HAVE_OPENCV_VIDEO
    init &= cv::initModule_video();
#endif
#ifdef HAVE_OPENCV_ML
    init &= cv::initModule_ml();
#endif
    #ifdef HAVE_OPENCV_CONTRIB
    init &= cv::initModule_contrib();
#endif

    if(!init)
        return -1;

May be related to this. Investigation is continuing. Any help will be acceptable..:)

2015-11-20 10:01:42 -0600 received badge  Autobiographer
2015-11-20 09:54:18 -0600 asked a question After 2.4.12.3 Windows Build from source - Java Unsatisfied Link Error

Hi,

I downloaded 2.4.12.3 source from github and build it on Ubuntu 14.04 64 bit and Windows 7 64 bit.

At linux side it is working and i saw that the problem of rendering H.264 video stream at high resolutions like 1920x1080 was solved. (Code change was : FFMPEG & GSTREAMER decoding through getting the frame with underlaying protocol TCP and null frame control)

But at Windows side although the build is okay on Visual Studio 2013 and the INSTALL project output seems true like below (i compared the folder structure with 2.4.9's and 2.4.11's sourceforge.net binary windows distributions), it gives always "Java Unsatisfied link error : opencv_java2412.dll can't find dependednt libraries)

image description

image description

image description

All required *.dll, *.lib files exist in bin and lib folders. I am sure about setting the -Djava.library.path as true because if you give the path wrong it gives another error like (nonative library found for opencv_2412)

So, does anybody have opinion about this situation? Did anybody try the compilation of 2.4.12.3 at Windows side?

Regards, Eray

2015-11-19 07:31:07 -0600 received badge  Necromancer (source)
2015-11-19 04:54:24 -0600 answered a question How to detect human using findcontours based on the human shape?

I tried this CPP code and adopt it to Java side as below. I changed some of the lines because no need of them. So, the last state of it with Java goes here. I used *.mp4 file but i resize the frame before processing because more than a 640x480 resolution, processing is too slow. I prefer to dynamic resize the frame to 320x240 or max. 640x480 before processing :

// Fields...

 Mat fg;     
 Mat blurred;
 Mat thresholded;
 Mat gray;
 Mat blob;
 Mat bgmodel;                                            

 BackgroundSubtractorMOG2 bgs;      

 List<MatOfPoint> contours = new ArrayList<MatOfPoint>();            

 CascadeClassifier body;

// Initializer part (you may put it to a private method and call it from constructor)...

     bgs = new BackgroundSubtractorMOG2();
     bgs.setInt("nmixtures", 3);
     bgs.setInt("history", 1000);
     bgs.setInt("varThresholdGen", 15);
     //bgs.setBool("bShadowDetection", true);   // This feautee does not exist on OpenCV-2.4.11
     bgs.setInt("nShadowDetection",0);                               
     bgs.setDouble("fTau", 0.5);                                         

     body=new CascadeClassifier("mapbase/hogcascade_pedestrians.xml"); // Standart sample pedestrian trained data file
      if(body.empty())  
      {  
           System.out.println("--(!)Error loading hogcascade file\n");  
            return;  
      }  
      else  
      {  
                 System.out.println("Pedestrian classifier loaded up");  
      }

// Frame reader part...

VideoCapture cam = new VideoCapture("D:\\Sample_Videos\\pedestrian-much.mp4"); // int 0 : Webcam, String : rtsp://IP:PORT/PATH for ip camera etc.
        if(cam.isOpened())  
          {  
           Thread.sleep(500); /// This one-time delay allows the Webcam to initialize itself  
           while( true )  
           {  
             cam.read(cam_image);  
             if(!cam_image.empty())  
              {            
                   // Thread.sleep(50); // If too much computation, you may give a time to CPU        
                   Imgproc.resize(cam_image, resizeImage, new Size(320, 240));
                   //Apply the classifier to the captured image  
                   renderImage=faceDetector.detect(resizeImage);
                   //Display the image  
                   BufferedImage img = facePanel.matToBufferedImage(renderImage);
                   facePanel.repaint();
                   renderImage.release();
              }  
              else  
              {   
                   System.out.println(" --(!) No captured frame from video source!");
                   break;   
              }  
             }  
            }
           cam.release(); //release the webcam   }

// Detect method which is called in while read frame loop... public Mat detect(Mat inputframe){

        Imgproc.resize(webcam_image, resizeImage, new Size(320, 240));
        Mat mRgba=new Mat();
        inputframe.copyTo(mRgba); // The frame read from video source (camera, *.mp4, *.avi file etc)
        blurred = new Mat();
        fg = blurred.clone();
        thresholded = fg.clone();
        blob = thresholded.clone();
        gray = blob.clone();

        Imgproc.cvtColor(mRgba,gray,Imgproc.COLOR_BGR2GRAY);  // Before processing we convert to gray
        Imgproc.GaussianBlur(gray,blurred,new Size(3,3),0,0,Imgproc.BORDER_DEFAULT); // Avoid from unnecessary pixels

             bgs.apply(blurred,fg); // Extract background from scene                             

            Imgproc.threshold(fg,thresholded,70.0f,255,Imgproc.THRESH_BINARY); // Apply max and min threshold

        Mat elementCLOSE = new Mat(5,5,CvType.CV_8U,new Scalar(255,255,255));
        Imgproc.morphologyEx(thresholded,thresholded,Imgproc.MORPH_CLOSE,elementCLOSE); // Do a body figure analysis (for object scene change optimization)

        Mat v = new Mat();
        Imgproc.findContours(thresholded,contours,v, Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE); // Find the changes of scene (the moving parts)

        Imgproc.equalizeHist(gray, gray); // Balance the contrast to gain proper result

        int cmin = 500; 
        int cmax = 80000;
        MatOfRect rects = new MatOfRect();
        if (contours != null && !contours.isEmpty()) {
            //if (contours.size() >= 50) { // You may use this control and a threshold value if too much contour you have
                //Imgproc.drawContours(mRgba,contours,-1,new Scalar(0, 0, 255),2); // If you want you ...
(more)
2015-04-16 11:05:49 -0600 answered a question IP camera video corruption - solutions?

Hi,

I think your code is right. Sample Java code is as below. I also tried on lots of environments like you. With Gstream ugly & good codecs on Ubuntu 12.04, H.264 streaming is working with a latency without any frame corruption.

The code is like that :

public static void main(String[] args) {

    ImageIO.setUseCache(false);
    JFrame jframe = new JFrame("ETS New Algorithm - Suzuki85");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel vidpanel = new JLabel();
    jframe.setContentPane(vidpanel);
    jframe.setSize(1280, 720);
    jframe.setVisible(true);

    Mat frame = new Mat();
    VideoCapture camera = new VideoCapture("rtsp://192.168.1.229:554/av0_1");
    while (true) {
        if (camera.read(frame)) {
                ImageIcon image = new ImageIcon(Mat2bufferedImage(frame));
                vidpanel.setIcon(image);
                vidpanel.repaint();
            }
    }
}

public static BufferedImage Mat2bufferedImage(Mat image) {
    MatOfByte bytemat = new MatOfByte();
    Highgui.imencode(".jpg", image, bytemat);
    byte[] bytes = bytemat.toArray();
    InputStream in = new ByteArrayInputStream(bytes);
    BufferedImage img = null;
    try {
        img = ImageIO.read(in);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        bytemat.release();
        bytes = null;
    }
    return img;
}

But if you need to process data (for example; motion tracking, histogram equalization etc. i mean Highgui procs) you need the Mat reference and this reference can not be gotten without FFMPEG. And OpenCV which is built from source with FFMPEG ability uses CPU more than 100% on Linux.So, you can not run properly on Linux. (Some folks said that they were succedded here and was me..:) but FFMPEG Needed not GStreamer but i couldn't get a stable environment)

So, i choosed to go on Win7 64 bit platform wt. OpenCV 2.4.11. Currently i can get MJPEG streming wt. 1920x1080 resolution at 16384 bitrate from my ip camera. Its quality is near to H.264 but data cost is twice of H.264. (H.264 = 500K, MJPEG = 800K) I also downloaded source code of OpenCV2.4.11 and open the project in Visual Studio .NET. I succedded to compile it and currently i am investigating the source code at CPP side if i will be able to find anything, i may write to here.

I saw that lots of codes exist at CPP side about comparing the FFMPEG versions. Sample like that :

image description

And lastly, you are right that MPEG4 or H.264 encoded files on disk are played without any error.

IP Camera H.264 640x480 RTSP Sample on Win7 64 bit and OpenCV2.4.11 (wt. Rendering errors) image description

H.264 Encoded MP4 File 640x480 Sample on Win7 64 bit and OpenCV2.4.11 (wt. out errors) image description

I guess the problem is related to network side. If it was related to FFMPEG, the H.264 encoded file also rendered wt. errors. GStreamer on Linux can render it wt. out errors with a latency. So, I think if grapb - retrieve cylec can wait some time about being sure that the frame is gotten properly than there will be no problem. I mean i can accept 2-3 seconds delay if Mat reference will be able to be given after reading the H..264 encoded video data.

See you on ... (more)

2014-11-10 14:05:09 -0600 received badge  Supporter (source)
2014-11-10 07:00:17 -0600 commented answer ip camera h264 error while decoding

I also compiled OpenCV2.4.9 under Ubuntu 14.04 without FFMEG support but while trying to render Video data through RTSP stream CPU usage raises more that 100% and computer gave no response.

After a research, i found that GStream codecs is no more supported from Ubuntu 14.04. I also ignored this situation and although i installed gstream bad-uglu-good codecs, CPU again went to over 100% and a dedlock occurred. So, i install GStream full compatible Ubuntu 12.04 LTS and compiled OpenCV2.4.9 without FFMPEG support also on it. I was carefull about not to install gstream1, i installed gstream0 codecs. After that i gained the 1920x1080 high reolution video stream from my ip camera. It is brillant and has only 1/2 second delay.

2014-10-26 17:15:12 -0600 commented answer ip camera h264 error while decoding

Hi,

Do you have OpenCV2.4.9 library which was built without FFMPEG support? I need the jar to try on Windows 7 64 bit. Would you please send the jar to this thread as an attachment or would you may email to me?

2014-10-18 08:43:32 -0600 received badge  Editor (source)
2014-10-18 06:52:54 -0600 asked a question RTSP link can not open Camera - Java H264 Stream

The problem is solved through this link : http://stackoverflow.com/questions/23199886/opencv-java-binds-videocapture-from-file-failing-silently After putting the OpenCV binaries path to Windowss' environment variable PATH and restart my Eclipse IDE, i will be able to get data from RTSP stream and any orher local file (for example avi) I can not post an answer because i am newbie and opencv prohibits me so i has just write to here. Regards, Eray

Hi,

I try this basic code as told in forums/internet but the isOpened() method always returns false. I tried InetSocketAddress to connect 192.168.1.229 on port 554 and this was succedded. Also i can see the camera video stream from iSpy software (a custom Camera control software) with URL rtsp://192.168.1.229:554/H264/streamtype=0. So, i am sure that the device is working properly.

What might be the problem? Any ideas..:(

Version : OpenCV 2.4.9 wt. Java 1.8/1.6

public static void main(String[] args) {

    int i=0;

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    VideoCapture capture = new VideoCapture("rtsp://192.168.1.229:554/H264/streamtype=0");

    Mat image=new Mat();
    double propid=0;
    capture.open("rtsp://192.168.1.229:554/H264/streamtype=0");

    if (capture.isOpened()) {
        System.out.println("Video is captured");
        int x=0;
        while (x<18) {
            x=x+1;
            propid=capture.get(x);
            System.out.println("Property Id is " + x + " value ="+ propid);
        }
        // grab 10 frames as a test
        while (i<10) {

        try {
            //capture.retrieve(image); //same results as .read()
            capture.read(image);    
        } catch (Exception e) {
            System.out.println("Null Pointer Exception during Read");
        }   
        i=i+1;
      }
    } else {
        System.out.println("Camera can not be opened!");
    }
    capture.release();
    System.out.println("VideoCapture is released"); 
    System.exit(0);
}