Ask Your Question

mark92m's profile - activity

2019-06-19 04:49:02 -0500 received badge  Popular Question (source)
2018-05-20 22:38:03 -0500 received badge  Famous Question (source)
2017-10-31 08:22:37 -0500 received badge  Notable Question (source)
2017-06-29 03:11:50 -0500 received badge  Popular Question (source)
2016-05-28 04:48:10 -0500 received badge  Student (source)
2016-04-30 13:58:37 -0500 asked a question Segmentation fault when copying Mat to struct

I am trying to fill a struct with Mat objects to pass to a thread.

   struct thread_data{
           int thread_id;
           int socket;
           uchar* receiveBuffer;

           // MAT IMAGE HEADERS:
           Mat _leftImage;
           Mat _rightImage;
           Mat _leftDepth;
           Mat _rightDepth;
           vector<Mat> _results;

Then I create an array of structs like this:

  struct thread_data td[MAX_THREADS];

Following, each struct gets populated, where the Mats in the struct are filled with Mats saved in Vectors :

for(i = 0; i < numThreads ; i++){
  td[i].thread_id = i;        
  td[i].socket = connected;         
  td[i].receiveBuffer = threadRecBuffer[i];

  td[i]._leftImage = leftImages[i];   
  td[i]._rightImage = rightImages[i]; 
  td[i]._leftDepth = leftDepth[i];    
  td[i]._rightDepth = rightDepth[i];     
  td[i]._results = resultsVector;           

Works well up to 9 iterations, and starts giving the error at the 10th iteration.

I get a segmentation fault 11 as soon as the first Mat is trying to get assigned.

Has anyone got any experience with passing Mats in structs ?

Note: All images exist and are being printed before hand.

Thanks in advance.

2016-04-16 05:11:55 -0500 asked a question Mat Data in Multithreading

In my code , I use sockets to offload processing of a Mat Image onto another machine, and wait for the result to be read back. So in end up with resulting the Mat Image.

I use fork() to create a child process to take care of the sending and receiving of this Mat image.

Mat image = imread( "image_source" );
Mat result;

if( child_process ){

      open socket();
      send( );
      receive( );

if( parent_process ){ 
    imshow(  result ) ;

The socket programming works fine and the child thread gets the correct information (verified). Obviously I need to access the resulting image from the parent thread and NOT the child thread only.

The parent thread keeps wait indefinitely, as Mat result is never populated.

Do I need to use some form of shared memory ? Or is that bad practice when using threads ?

2016-04-13 04:55:42 -0500 asked a question Is it possible to create an OpenCL Kernel that uses OpenCV?

I have an OpenCV application which I would like to speed up using several GPUs (located in a linux machine cluster).

My approach was to create an OpenCL kernel to execute on a single GPU, and then extend this performance by using VirtualCL to use the network-attached devices.

My question is, is it possible to run an OpenCL kernel that somehow calls the OpenCV Library ?

clBuildProgram() allows you to specify compiler options which include the include file directories. The header file includes, should look something like: -I myincludedir1 -I myincludedir2

similar to:

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

Has anyone ever managed to create an OpenCL kernel that uses OpenCV ? Or is there any simpler way ? (without using UMat )

2016-04-04 05:18:53 -0500 asked a question Displaying image results in real time

I want to process a set of images and display them in real time in order to time the frames per second with which the code executes.

    for(int i = 0 ; i  < numOfImages ; i ++ ){


        imshow("Frame", image); 

Is the waitKey(1) the most efficient way to display the succession of frames as fast as possible ? Or is there a better way ?


2016-04-01 03:28:08 -0500 received badge  Enthusiast
2016-03-24 14:46:11 -0500 commented answer index matrix

Indeed it was quicker, thank you both for your help.

2016-03-24 09:31:17 -0500 commented question index matrix

I need to use a warping algorithm. Given an image (the Mat mentioned above), each pixel has a depth value ( stored in another Mat). The algorithm takes a set of coordinates and warps them to new coordinates.

So for each row of this matrix, I shall calculate a new set of values.

I wish to parallelise this process. Hence why i have isolated it into a separate process.

2016-03-24 07:13:18 -0500 asked a question index matrix


In my code I need to create a matrix filled with all the possible pixel coordinates of an image ( Mat ).

ex.for a 1024x768 image: [ 0, 0] [ 0, 1] [ 0, 2] . . . [1024, 768]

Is there a more efficient way of doing this than:

void indexMatrix(Mat &matrix, int height, int width){

int cols = height*width;
int index = 0;

matrix = Mat::ones(cols, 2, CV_8UC1);

for( int col = 0; col < width; ++col ) {
    for (int row = 0; row < height; ++row) {<uchar>(index,0) = col;<uchar>(index,1) = row;

2016-03-17 17:34:08 -0500 commented question Apply function to each Mat element

... cause I've been looking "deeply" at it for quite some time today, but I'm still getting used to how OpenCV operates. I think i understand what you mean.

2016-03-17 17:25:36 -0500 commented question Apply function to each Mat element

So you suggest first dividing all elements by 255, then negating it, then adding 1, then using the mul() function , and so on ... for each step of the algorithm ?

2016-03-17 17:14:24 -0500 asked a question Apply function to each Mat element

Does anyone know of a more efficient OpenCV practice to perform the following operation on each Mat element ? Each element undergoes the same function.

Mat calcDepthValues(Mat &depth){

Mat realDepth= depth.clone();

Mat_<double>::iterator it = realDepth.begin<double>();
Mat_<double>::iterator itend = realDepth.end<double>();

for (; it != itend; ++it) {

    (*it) = 448.251214 +  ((1- (*it)/255)  * 10758.02914);

return realDepthValues;
2016-03-17 17:07:44 -0500 received badge  Supporter (source)
2016-03-17 17:07:35 -0500 received badge  Scholar (source)
2016-03-17 13:10:25 -0500 commented answer Parallel implementation of per pixel calculations

I have edited my question to include a more complex example.

2016-03-17 13:08:18 -0500 received badge  Editor (source)
2016-03-17 12:43:13 -0500 commented answer Parallel implementation of per pixel calculations

Thanks for the very beneficial tip.

But not all my operations are this simple.

I Have cases when i require a pixel(x, y) from Mat A, another pixel(x, y) from Mat B, and input these pixel values into a specific algorithm. Then I store the result into pixel(x, y) of Mat C.

I don't know how to proceed with such complex situations.

Would it be more helpful if I add some example code ?


2016-03-17 12:18:56 -0500 asked a question Parallel implementation of per pixel calculations

Hi, I am using OpenCV 3.0 .

In my serial implementation I use the Mat_::iterator quite often to access and edit each pixel of an image (As in the example below).

What is the best practice to accelerate these calculations ? Unfortunately I cannot find ways to access particular pixels in a UMat matrix.

Mat createFGMask(Mat &depthMap){
Mat fgMask = depthMap.clone();

// obtain iterator at initial position
Mat_<uchar>::iterator it= fgMask.begin<uchar>();
// obtain end position
Mat_<uchar>::iterator itend= fgMask.end<uchar>();

// loop over all pixels
for ( ; it!= itend; ++it) {
    if( (*it)> 120 ) (*it)=255;
    else (*it)= 0;

return fgMask;


A more complex scenario where values of each pixel is required:

Mat pixelShifting(Mat &refImage, Mat &depthValue, ){

Mat warpedImage = Mat::zeros( 768, 1024, CV_8UC3 );

int height = refImage.rows;
int width = refImage.cols;

double newX, newY;
Mat newCoord;

for( int col = 0; col < refImage.cols; ++col ){
    for( int row = 0; row < refImage.rows; ++row ){

        if(<uchar>(row,col) <= 120 ){

            newCoord = calcNewCoord(col, row,<double>(row,col));                                                                                  

            newX =<double>(0, 0);
            newY =<double>(1, 0);

            if( 0<= newY < height && 0<= newX < width ) {

      <Vec3b>(newY, newX) =<Vec3b>(row, col);

return warpedImage;


This code basically takes 2 images, and populates a 3rd image with pixels of image1 by calculating new coordinates using values from image 2.