Ask Your Question

George Profenza's profile - activity

2020-07-17 19:02:56 -0500 marked best answer How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)?

Please note this is a cross-post also present on stackoverflow, but I would also like the expertise of this wonderful expert community.

This question is very similar to this one however it's on Linux ARM ( Raspberry Pi).

I've compiled OpenCV 4.4.0 from source along with the Java bindings and tried something like this hack (that worked on Windows):

import org.opencv.core.*;

public class CVTest {

        public static void main(String[] args) {
                System.out.println("setup");
//              loading the typical way fails :(
//              System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

                System.load("/home/pi/eclipse/CVTest/lib/libopencv_core.so");
////System.load("/home/pi/opencv/build/lib/libopencv_core.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_imgproc.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_imgcodecs.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_img_hash.so");
////System.load("/home/pi/eclipse/CVTest/lib/opencv_core.so");//videoio_ffmpeg440_64.dll
//System.load("/home/pi/eclipse/CVTest/lib/libopencv_videoio.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_photo.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_xphoto.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_flann.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_features2d.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_calib3d.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_phase_unwrapping.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_structured_light.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_xfeatures2d.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_video.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ximgproc.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_aruco.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_bgsegm.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_bioinspired.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_objdetect.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_face.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_dnn.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_tracking.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_plot.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ml.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ml.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_text.so");
// crashes here!
System.load("/home/pi/eclipse/CVTest/lib/libopencv_java440.so");


Mat m = Mat.eye(new Size(3,3), CvType.CV_8UC1);
                System.out.println("done");
        }

}

However, depending on the Raspberry Pi, I get different crashes at the same line, loading libopencv_java440 (after the other dependant libraries have loaded):

On a Raspberry Pi 3B running Raspian stretch I get errors like this one:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGBUS (0x7) at pc=0x6360f644, pid=9730, tid=0x64eba470
#
# JRE version: Java(TM) SE Runtime Environment (8.0_202-b08) (build 1.8.0_202-b08)
# Java VM: Java HotSpot(TM) Client VM (25.202-b08 mixed mode linux-arm )
# Problematic frame:
# C  [libopencv_core.so+0x258644]  cv::Ptr<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::~Ptr()+0x38
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java ...
(more)
2020-07-17 15:49:30 -0500 received badge  Self-Learner (source)
2020-07-17 14:53:12 -0500 answered a question How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)?

The solution was to reinstall Raspian OS which came with an updated c++ compiler. Details here

2020-07-04 14:02:20 -0500 commented question How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)?

@berak thank you for the suggestion. I did try that: unfortunately I still get segfault. I'll try disabling NEON/VFP3 op

2020-07-03 18:14:03 -0500 asked a question How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)?

How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)? Please note this is a cross-post also present on

2018-07-25 07:14:40 -0500 received badge  Notable Question (source)
2017-11-17 10:00:10 -0500 received badge  Popular Question (source)
2017-10-05 01:45:58 -0500 received badge  Famous Question (source)
2017-03-14 01:19:56 -0500 received badge  Popular Question (source)
2016-01-11 06:26:54 -0500 received badge  Notable Question (source)
2015-10-12 08:27:21 -0500 received badge  Notable Question (source)
2015-07-12 09:19:47 -0500 received badge  Taxonomist
2014-12-09 13:44:50 -0500 marked best answer How can I measure distances with OpenNI ?

Hello, I'm playing with the built in OpenNI access within OpenCV 2.4.0 and I'm trying to measure the distance between two points in the depth map. I've tried this so far:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

Point startPt(0,0);
Point endPt(0,0);

void onMouse( int event, int x, int y, int flags, void* )
{
    if( event == CV_EVENT_LBUTTONUP) startPt = Point(x,y);
    if( event == CV_EVENT_RBUTTONUP) endPt   = Point(x,y);
}

int main( int argc, char* argv[] ){
    VideoCapture capture;
    capture.open(CV_CAP_OPENNI);
    //capture.set( CV_CAP_PROP_OPENNI_REGISTRATION , 0);
    unsigned t0=clock();
    if( !capture.isOpened() ){
        cout << "Can not open a capture object." << endl;
        return -1;
    }
    unsigned elapsed=clock()-t0;
    cout << "initialized in "<< elapsed <<" s. ready!" << endl;
    namedWindow( "depth", 1 );
    setMouseCallback( "depth", onMouse, 0 );
    for(;;){
        Mat depthMap;
        if( !capture.grab() ){
            cout << "Can not grab images." << endl;
            return -1;
        }else{
            Mat show,real,valid;
            if( capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ) ){
                depthMap.convertTo( show, CV_8UC1, 0.05f);
            }
            capture.retrieve(valid,CV_CAP_OPENNI_VALID_DEPTH_MASK);
            if( capture.retrieve(real, CV_CAP_OPENNI_POINT_CLOUD_MAP)){
                unsigned int sp = valid.at<unsigned char>(startPt.x, startPt.y);
                unsigned int ep = valid.at<unsigned char>(endPt.x, endPt.y);
                if(sp == 255 && ep == 255){
                    Vec3f s = real.at<Vec3f>(startPt.x, startPt.y);
                    Vec3f e = real.at<Vec3f>(endPt.x, endPt.y);
                    float dx = e[0]-s[0];
                    float dy = e[1]-s[1];
                    float dz = e[2]-s[2];
                    float dist = sqrt(dx*dx + dy*dy + dz*dz);
                    putText(show,format("distance: %f m\n",dist),Point(10,10),FONT_HERSHEY_PLAIN,1,Scalar(255));
                }
            }
            circle(show,startPt,3,Scalar(255),3);
            circle(show,endPt,3,Scalar(192),3);
            line(show,startPt,endPt,Scalar(128));
            imshow("depth",show);
        }
        if( waitKey( 30 ) >= 0 )    break;
    }

}

but I have a few issues:

  1. Unless I check for values in the valid depth mask first the program crashes sometimes (I imagine due to bad data)
  2. As far as I understand the point cloud map returns XYZ data in meters (CV_32FC3), but the numbers I see on screen look wrong.

Am I retrieving the xyz values and doing the distance calculations correctly ? What am I missing out ?

Thanks, George

2014-12-09 13:44:43 -0500 marked best answer How to use multiple depth sensors with OpenNI/OpenCV ?

I'm using OpenCV 2.4.4 built from source with OpenNI support and I'd like to use two Asus sensors at the same time. Is that possible with OpenCV ? If so how ?

If I initialize a sensor like so:

VideoCapture sensor1;sensor1.open(CV_CAP_OPENNI_ASUS);

How can I init the second ?

2014-12-09 13:15:54 -0500 marked best answer How to use createButton ?

I'm currently going through the samples and looking at how to add GUI elements. Starting from connectedcomponents.cpp I've managed to get understand how a trackbar works. Now I'm trying to add a button. Here's a basic snippet:

void on_button(int, void*){
    cout << "click" << endl;
}
//in main
createButton("test",on_button);

Before I got some link issues related to QtCore/QtGui. I've updated the OpenCV port to 2.4.2 with qt4 and the link error is gone and I can see some nice Gui elements in the namedWindow, but I can't see the button I created. Any hints/tips on what I'm doing wrong ?

Thanks!

2014-07-13 09:15:57 -0500 received badge  Popular Question (source)
2014-07-12 18:23:26 -0500 received badge  Famous Question (source)
2014-07-04 19:47:41 -0500 received badge  Popular Question (source)
2014-04-13 17:05:02 -0500 commented answer WITH_WIN32UI Errors compiling OpenCV 2.4.8 with mingw

This worked for me too. Now I'm getting some weird crashes at runtime though, but that's something else(for another question). Thanks(+1)

2014-04-12 12:44:49 -0500 received badge  Self-Learner (source)
2014-04-12 12:28:06 -0500 answered a question How to get latest OpenCV on raspberry pi ?

As an additional option you can have a look at my RPI-CV-Goodies repo. In the releases it includes the OpenCV2.4.8 sources already compiled so all you need to do is download(~357MB), untar then

cd build
sudo make install

Also I have a couple of other handy useful Raspberry PI OpenCV tools. PiCapture is a cv::Mat wrapper for the Pi Camera module and it includes camera settings too.

2014-04-12 11:38:43 -0500 answered a question Can I get away without converting BGR2RGB in my case ?

Upgrading to the latest firmware using

sudo rpi-updates

enables an addition encoding mode: MMAL_ENCODING_RGB24 which returns colour frames in the expected format.

For more details see my PiCapture repo

2014-04-09 18:20:49 -0500 marked best answer How to change BackgroundSubtractorMOG2 options

I'm learning OpenCV and loving it so far. Currently I'm looking into background subtractor and it's awesome that the library already offers algorithms for this. I was looking at BackgroundSubtractorMOG2 and I've managed to get a basic setup running:

cv::BackgroundSubtractorMOG2 bg;
//in update loop
bg.operator ()(frame,foreground);
bg.getBackgroundImage(background);

but I haven't worked out how change/use options with for this class. The documentation lists members to control the algorithm (such as nmixtures,backgroundRatio,etc.), but they appear to be protected (in OpenCV 2.4.1). Would I still be able to control the algorithm using OpenCV 2.4 ? If so, how ?

Thanks!

2014-04-04 13:22:20 -0500 received badge  Editor (source)
2014-04-04 13:11:15 -0500 asked a question Can I get away without converting BGR2RGB in my case ?

I'm working on a little opencv wrapper for the Raspberry PI, mostly using Samarth Brahmbhatt's code from Practical OpenCV.

In his code though he uses YUV encording, merges each channel than converts YUV2BGR. I'm using MMAL_ENCODING_BGR24 in the hope of skipping the conversion step.

Does imshow display BGR matrices as RGB ? My guess was yes, but now I'm slightly confused.

I initialize the cv::Mat with MMAL data like so:

 MMAL_BUFFER_HEADER_T *new_buffer;
   mmal_buffer_header_mem_lock(buffer);
   unsigned char* pointer = (unsigned char *)(buffer -> data);
    Mat clr(PiCapture::height, PiCapture::width, CV_8UC3, pointer);

But imshow displays it like so:

BGR

I have to do

cvtColor(clr,clr,BGR2RGB);

so imshow displays the image as I expect it(RGB):

RGB

My other assumption is, maybe I'm expecting MMAL_ENCODING_BGR24 to return data in BGR and it could be RGB ? (and the cvtColor(clr,clr,BGR2RGB) call has the same result as cvtColor(clr,clr,RGB2BGR): moves the data in the same way, if the images was RGB initially, not BGR, it would get converted to BGR ) ?

In either case, could I somehow initialize a cv::Mat and pass the data in the right format in one go (without converting) ? Is there a way to check the image colour space ?

Thanks

2014-03-25 19:50:29 -0500 asked a question WITH_WIN32UI Errors compiling OpenCV 2.4.8 with mingw

I'm trying to compile OpenCV 2.4.8 on Windows 8 with mingw but got stuck on this errors:

[ 36%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/win
dow_w32.cpp.obj
cd /d C:\Users\HM\Downloads\opencv-2.4.8\opencv\buildMingW32\modules\highgui &&
C:\MinGW\bin\g++.exe   -DCVAPI_EXPORTS -DHIGHGUI_EXPORTS -fsigned-char -mstackre
align -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wform
at -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-
arith -Wshadow -Wsign-promo -fdiagnostics-show-option -march=i686 -fomit-frame-p
ointer -msse -msse2 -mfpmath=sse -ffunction-sections -Wno-deprecated-declaration
s -O2 -DNDEBUG  -DNDEBUG @CMakeFiles/opencv_highgui.dir/includes_CXX.rsp    -inc
lude "C:/Users/HM/Downloads/opencv-2.4.8/opencv/buildMingW32/modules/highgui/pre
comp.hpp" -Winvalid-pch  -o CMakeFiles\opencv_highgui.dir\src\window_w32.cpp.obj
 -c C:\Users\HM\Downloads\opencv-2.4.8\opencv\sources\modules\highgui\src\window
_w32.cpp
C:\Users\HM\Downloads\opencv-2.4.8\opencv\sources\modules\highgui\src\window_w32
.cpp: In function 'void cvSetModeWindow_W32(const char*, double)':
C:\Users\HM\Downloads\opencv-2.4.8\opencv\sources\modules\highgui\src\window_w32
.cpp:477:71: error: 'MonitorFromRect' was not declared in this scope
C:\Users\HM\Downloads\opencv-2.4.8\opencv\sources\modules\highgui\src\window_w32
.cpp: In function 'LRESULT MainWindowProc(HWND, UINT, WPARAM, LPARAM)':
C:\Users\HM\Downloads\opencv-2.4.8\opencv\sources\modules\highgui\src\window_w32
.cpp:1355:69: error: 'MonitorFromRect' was not declared in this scope
mingw32-make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_w
32.cpp.obj] Error 1
mingw32-make[2]: Leaving directory `C:/Users/HM/Downloads/opencv-2.4.8/opencv/bu
ildMingW32'
mingw32-make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2

mingw32-make[1]: Leaving directory `C:/Users/HM/Downloads/opencv-2.4.8/opencv/bu
ildMingW32'
mingw32-make: *** [all] Error 2

If I uncheck with WITH_WIN32UI in CMake the library gets build, but anything using windows from highgui crashes at runtime. Any tips on I could move on ?

2013-09-11 11:01:07 -0500 answered a question adding new device for image capture (Raspberry Pi CSI module)?

I was wondering the same thing.

The closest thing I found was this article.

It's not official opencv integration though. Looking forward to see what other options are out there.

2013-07-31 17:49:24 -0500 commented answer How to control a KalmanFilter using the Java interface ?

hmmm, I might need to go the CV_WRAP option. Where would I make that change ? Would that be in Mat.java in the java wrapper ? Also, I've tried commenting out as much as possible to get the sample to bare minimum (code here) but it still looks like I need to access setIdentity(KF.measurementMatrix); and other KF properties. Thanks for the suggestions(+1), looking forward to see your suggestion

2013-07-30 18:08:38 -0500 asked a question How to control a KalmanFilter using the Java interface ?

I'm trying to port the code in this KalmanFilter tutorial to Java.

I have no clue how to write this in Java:

KF.statePre.at<float>(0) = mouse_info.x;
KF.statePre.at<float>(1) = mouse_info.y;
KF.statePre.at<float>(2) = 0;
KF.statePre.at<float>(3) = 0;
KF.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,0,0,   0,1,0,0,  0,0,1,0,  0,0,0,1);

I couldn't find any fields or any setters for statePre or transitionMatrix.

The javadocs state:

The class implements a standard Kalman filter http://en.wikipedia.org/wiki/Kalman_filter, [Welch95]. However, you can modify transitionMatrix, controlMatrix, and measurementMatrix to get an extended Kalman filter functionality. See the OpenCV sample kalman.cpp.

but I'm guessing this is valid for c++, not java.

Is there a way to control the KalmanFilter in Java ? If so, how ?

2013-06-26 23:40:18 -0500 received badge  Notable Question (source)
2013-03-20 13:51:42 -0500 received badge  Popular Question (source)
2013-03-03 16:35:28 -0500 marked best answer CascadeClasifier detectMultiScale arguments

Hello, I'm an opencv/c++ noob so please bare with me. I was looking at the objectdetect sample and tried it with a couple of body detection cascades(haarcascade_fullbody/haarcascade_upperbody/haarcascade_lowerbody,haarcascade_mcs_upperbody) on some footage I took with a camera of a pedestrian bridge. So far so good, but it's a demanding functionality.

I though thinks might run smoother if I pass the min/max Sizes since my camera is always in the same place and I figure out the min/max bounding boxes. Unfortunately I ran into syntax errors when trying to do so:

//my call
body.detectMultiScale( gray, bodies, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30),cv::Size(0,0),cv::Size(5,13),cv::Size(45,80));
//errors:
main.cpp:43: error: no matching function for call to 'cv::CascadeClassifier::detectMultiScale(cv::Mat&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size, cv::Size, cv::Size, cv::Size)'
/opt/local/include/opencv2/objdetect/objdetect.hpp:383: note: candidates are: virtual void cv::CascadeClassifier::detectMultiScale(const cv::Mat&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size, cv::Size)
/opt/local/include/opencv2/objdetect/objdetect.hpp:393: note:                 virtual void cv::CascadeClassifier::detectMultiScale(const cv::Mat&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, int, int, cv::Size, cv::Size, bool)

I simply added two cv::Size objects as the min max, but to be honest I'm not sure: 1. Why the previous arguments are Size() objects as well when the docs list them as ints ? 2. Am I looking at the wrong docs or at the docs in a wrong way ?

C++: void CascadeClassifier::detectMultiScale(const Mat& image, vector<rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())

from docs.

Would this improve detection by much ? Also, I'm thinking about running the detection every few frames (not all the time) and track the detected points. What functions should I look for to tell OpenCV to track specified coordinates ?

Thanks!