Ask Your Question

Richard Y. Hayashi's profile - activity

2018-07-27 09:17:15 -0500 received badge  Teacher (source)
2018-03-12 10:15:50 -0500 received badge  Popular Question (source)
2015-03-04 01:58:38 -0500 commented answer How to use Mat as a C++ member correctly?

Thank you everyone for your help and suggestions. Merci @thdrksdfthmn. I found where the problem was just like @thdrksdfthmn had posted. I made the mistake of not making the destructor of the base class of CameraImage, which I forgot to add in my post, a virtual destructor; I'm using a pointer to the base class when I delete the object. It is now working fine. I hope the code I have posted can be of help to others.

2015-03-02 21:27:02 -0500 commented answer How to use Mat as a C++ member correctly?


I got more detail of the "Unknown Exception" by using cv::Exception at Mat creation and it confirmed what I thought: Failed to allocate 45453316 bytes in function cv::OutOfMemoryError. This is using just the creating and deleting of CameraImage in the Grab function and not placing it in the queue.

2015-03-02 18:09:04 -0500 commented answer How to use Mat as a C++ member correctly?

I am trying to store the Mat in an object placed in a queue to be written out to disk by another thread. The Write thread's job is to just write the image to disk, and delete the object with the Mat, since writing to disk may take longer than getting the next image/frame depending on fps, size, and image file type. Right now I am writing the the colour image, 4608 x 1400 at 2 fps, as a BMP which is written to disk at about 200 ms so the queue does not fill up.

It would seem that the Mat does not deallocate the data memory when the container object is deleted, since I keep running into an Unknown Exception on creating the 80th or so grabbed image. What I meant to say was, I do not get this problem when I write the image immediately ... (more)

2015-03-02 05:43:11 -0500 commented answer How to use Mat as a C++ member correctly?

By the way, I was able to write the Mat image out to disk in the grab thread without any problems, but it dies after a certain number of images, and depending of course on the size of the image, when it comes to adding it to an object and putting it in the queue to be written later.

2015-03-02 05:35:52 -0500 commented answer How to use Mat as a C++ member correctly?

I gave your suggestion a try, but it is still giving me an "Unknown Exception" when I go to create a new Mat for the around 75th image grabbed now. I first tried just creating the CameraImage then deleting it without putting it in the queue. Then when I put it in the queue it gives an "Unhandled Exception" with "Access violation reading location xxx" when it comes to writing it to disk. Either way it give me Unknow Exception on create.

2015-03-02 02:21:29 -0500 asked a question How to use Mat as a C++ member correctly?

Using VS 2012 with OpenCV 2.4.10

I am grabbing an image from a Basler camera in a thread class in my DLL and and converting it to an OpenCV Mat. I then pass it to an CameraImage class that is created using new and add it to a queue where another thread writes it out to disk and deletes the instance.

I find that whether I assign, clone(), copyTo(), or create/memcpy image to CameraImage.mImage when I delete the instance of CameraImage, mImage does not delete the image data and around the 86th image the create Mat throws an exception which means that it cannot allocate the memory for the new image. I thought the smart pointer takes care of the data memory allocation/deallocation automatically as well as refcount. I even call mImage.release() in the CameraImage destructor. I have used assert() to check on the refcount(s) (not shown).

Image Grab function called by thread

void CImageWriter::Grab()

   cv::Mat image;

   ConvertToMat(image, result.GetImage());
   CameraImage *cameraImage = new CameraImage(image);

   // Send it to the write queue thread.

Converting to Mat

void CImageWriter::ConvertToMat(cv::Mat &image, Pylon::IImage &pylonImage, ImageRotation rotateDegrees)
   Pylon::CImageFormatConverter converter;
   Pylon::CPylonImage target;

   converter.OutputPixelFormat = Pylon::PixelType::PixelType_BGR8packed;
   converter.OutputBitAlignment = Pylon::OutputBitAlignmentEnums::OutputBitAlignment_MsbAligned;
   converter.Convert(target, pylonImage);

   image.create(target.GetHeight(), target.GetWidth(), CV_8UC3);
   // copies from Result.Buffer into img 
   memcpy(image.ptr(), target.GetBuffer(), 3*target.GetWidth()*target.GetHeight());

   Rotate(image, image, rotateDegrees);

The Image Class

class CameraImage
    CameraImage(cv::Mat &image);

    void WriteToDisk(void);

    cv::Mat mImage;

 CameraImage::CameraImage(cv::Mat &image)
    mImage = image; or mImage = image.clone();  or  image.copyTo(mImage); 
    mImage.create(image.rows, image.cols, CV_8UC3);
    memcpy(mImage.ptr(), image.ptr(), 3 * image.rows * image.cols);


Is this the proper way to use Mat as a C++ member? What am I doing wrong?

2015-02-24 19:02:26 -0500 commented question Exception when copying from .data

Also if there is a better way to do this then please educate me.

2015-02-24 19:00:52 -0500 asked a question Exception when copying from .data

Has anyone had an exception thrown when using CopyMemory() or memcpy_s() to copying from a to shared memory? Using C++ and MFC, I open shared memory using CreateFileMapping() and MapViewOfFile() to get converted images from the camera grab thread to other threads wanting the camera image data to display, say onto a Picture Control.

[other conversions here and a copy to save to file]
cv::cvtColor(image, image, CV_BGRA2RGBA);  // Windows bitmap friendly format
CopyMemory(mMemory,, mImageInfo.imageSize);    
memcpy_s(mMemory, sizeof(BYTE),, mImageInfo.imageSize);

But after a few dozen memory transfers, something like 48 images, an exception is finally thrown as shown: image description

Can anyone help me to understand why this is happening? How can just copying from cause this exception after a few dozen "successful" transfers? I still have yet to see if the data is there correctly, but when I comment the memory copying line the exception does not happen so I know it is happening there.

2015-02-24 18:02:19 -0500 received badge  Enthusiast
2015-02-20 05:50:44 -0500 received badge  Necromancer (source)
2015-02-20 05:34:18 -0500 received badge  Supporter (source)
2015-02-20 05:32:55 -0500 received badge  Editor (source)
2015-02-20 05:32:11 -0500 answered a question Showing Mat image in Picturebox

After a day or two struggling to get an OpenCV Mat to Windows Bitmap I came across a simple solution after trying so many different things found on the Net. Try this:

Mat matImage = imread("image.bmp", CV_LOAD_IMAGE_COLOR);
cvtColor(matImage, matImage, CV_BGRA2RGBA);

hBitmap = CreateBitmap(matImage.cols, matImage.rows, 1, 32,;

And then you have a Bitmap to use in the Picture Control/Box. This is for MFC but I think it should be similar for C++/CLI. It was an ah-ha moment.

2015-02-19 17:56:17 -0500 commented question How to detect labels with numbers in a black rectangle?

The images are real time from the camera and will be similar distance to that in the picture. The system will check all the numbers in view and shine a laser on the one that is being searched for.if it is there. It's to help people quickly find certain parts by number among others on the shelf. Thanks. Hope to return the favour.

2015-02-17 21:57:08 -0500 asked a question How to detect labels with numbers in a black rectangle?

I am an absolute newbie to OpenCV and Computer Vision with only a few weeks of trying to understand this fascinating subject. Please excuse me if my question is "stupid" or otherwise, but we all have to start somewhere. What might be trivial to an expert or Guru is a mountain to a beginner until they are enlightened with the knowledge.

With that aside, I am involved in a project to locate labels with a series of numbers in a black rectangle (see attached photo).

image description

Using the images from a camera, I need to detect and retrieve the numbered rectangles to apply OCR on it and shine a laser pointer on the number that is being searched for. I am able to manually get the rectangle, adjust for its perspective, binarize it and use Tesseract to read it. But I am having difficulty in detecting the label and getting the bounding rectangle of the numbers. I have tried using Cascade Classifiers without much luck and applying adaptive thresholding to get a clearer image of the bounding black rectangle. What would be the best approach to getting the ROI? Is it possible to detect the label using Cascade Classifiers? If it is then please enlighten me as to how train the Cascade Classifier properly because I have followed the instructions, found on the Net, to train them. Do I need to get clearer/cleaner images of the labels and what size is best for the images? I found out there is a limit to how big the positive images can be.

Your suggestions, ideas, constructive comments, and help is greatly appreciated. And I hope to pass on what I learn to others also.