MATLAB MSER Feature detection not working - image-processing

I am using the detectMSERFeatures function in the computer vision toolbox of MATLAB and have been running into a few errors. I have a black and white image that I am reading in to detect the features of, however I want to invert the image before running the feature detection or I am filtering for the red in an image. Therefore, either way I have a binary image that I am trying to use in detectMSERFeatures. I know that does not work, but I have tried several conversions to a usable format and none of them have seemed to work. detectMSERFeatures will pick up features if I use rgb2gray on the original image, but not if I try to convert it. Here is everything I have tried so far:
Target1=imread('Decal0.JPG');
Target1bw=~im2bw(Target1);
Target=uint8(Target1bw);
[m,n]=size(Target);
regionsTarget = detectMSERFeatures(Target, 'MaxAreaVariation',0.15,...
'ThresholdDelta',15, 'RegionAreaRange',[10000 (m*n)/2]);
Target1=imread('Decal0.JPG');
Target1bw=~im2bw(Target1);
Target=im2double(Target1bw);
regionsTarget = detectMSERFeatures(Target, 'MaxAreaVariation',0.15,...
'ThresholdDelta',15, 'RegionAreaRange',[10000 (m*n)/2]);
Target1=imread('Decal0.JPG');
Target1bw=~im2bw(Target1);
Target2=255*Target1bw;
[m,n]=size(Target2);
Target3=zeros(m,n,3);
Target3(:,:,1)=Target2;
Target3(:,:,2)=Target2;
Target3(:,:,3)=Target2;
Target3=uint8(Target3);
Target=rgb2gray(Target3);
regionsTarget = detectMSERFeatures(Target, 'MaxAreaVariation',0.15,...
'ThresholdDelta',15, 'RegionAreaRange',[10000 (m*n)/2]);
What have I done incorrectly?

I brought the question up to Mathworks and it was a bug in MATLAB. Here is their response:
"We have detected a bug in detectMSERFeatures when it handles binary images. A workaround would be is to use regionprops to detect the regions for binary images. Then, MSERRegions can be constructed as follows:
props = regionprops(im2bw(newGrayTarget),'PixelList');
pixlist = {}
for i = 1:numel(props)
pixlist = [pixlist; int32(props(i).PixelList)]; end
r = MSERRegions(pixlist);
Thanks for the help!

Related

Conversion between opencv and javacv

I'm thinning an binary image. I found code done by using opencv. Here is the code.
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
I'm trying to convert this code to java via javacv. Both erosion and dilation completed successfully as follows.
skelImg=cvCloneImage(otsuImg);
cvErode(otsuImg, skelImg, null, 1);
cvDilate(skelImg, skelImg, null, 1);
But I couldn't find javacv code for cv2.subtract(IplImage,IplImage). Can someone help me out with this?
There is one subtract function in Core. See this for better understanding
Rather than use the javacv wrapper to OpenCV's deprecated API, consider using the following methods from OpenCV's official Java API:
org.opencv.imgproc.Imgproc.erode
org.opencv.imgproc.Imgproc.dilate
org.opencv.core.Core.subtract
org.opencv.core.Core.bitwise_or

Opencv - create png image

As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming.
Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.
The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project.
Firstly, some code:
websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols * websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;
but I am getting strange error during execution of the second line:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
and I am a bit confused why I am getting this error from my code.
Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMatto get all of the png encoded data?
The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. You are passing 0, which is the same thing as NULL. That's why you are getting the message NULL not valid.
You should probably use this:
websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);
You can check out the documentation of cvEncodeImage here.
You can check out some examples of cvEncodeImage, or its C++ brother imencode here: encode_decode_test.cpp. They also show some parameters you can pass to cvEncodeImage in case you want to adjust them.

Implementing bag of word algorithm from opencv sample codes

I am trying to implement bagofwords_classification.cpp from opencv version 2.4.5 sample codes.cpp . What are the changes that we are required to make in this .cpp file for proper working of code. I am new to opencv and still trying sample codes.
How and where to add the Feature detector,descriptor extractor, descriptor matcher ?? in that .cpp code
Whenever i debug any code it never display results but just output the info about what that .cpp file is gonna do. In (EXAMPLE) matching_to_many_images.cpp even the images are saved in the file but still no results are shown.
To show an image, you can use cvShowImage("Title",image) or imshow(). This depends on wheter to image is an IplImage or Mat.
The code example is not 'false', the program uses commandline arguments, thus to start it you need to add certain commands.
From the code
[feature detector]
Feature detector name (e.g. SURF, FAST...) - see createFeatureDetector() function.
[descriptor extractor]
Descriptor extractor name (e.g. SURF, SIFT) - see createDescriptorExtractor() function.
[descriptor matcher]
Descriptor matcher name (e.g. BruteForce) - see createDescriptorMatcher() function.
then from those arguments it calls
Ptr<FeatureDetector> featureDetector = createFeatureDetector( ddmParams.detectorType );
Ptr<DescriptorExtractor> descExtractor = createDescriptorExtractor( ddmParams.descriptorType );

JavaCV Stitching

I am trying to stitch multiple images by using JavaCV 0.1 and OpenCV 2.4.0 in Java, i use this code for stitching images :
stitcher = Stitcher.createDefault(false);
MatVector images = new MatVector(imageN.size());
for(...){
CvArr image = cvLoadImage(imageN);
images.put(index,image);
}
MatVector result = new MatVector(1);
int status = stitcher.stitch(images,result);
if( status == stitcher.OK )
{
cvSaveImage(result.getIplImage(0));
}
NOTE 1 : Loaded images in this example are valid image for stitching.
NOTE 2 : C++ version of the code runs with no problem on current configuration
In stitcher.stitch method opencv throws an assertion exception such as "k == MAT". How should i fix this? Is MatVector usage is right in this sample code?
Thanks...
I found it, it is a bug related with JavaCv.
Actually JavaCv is not guilty.OpenCV stitcher API uses cv::OutputArray for returning stitched image but this method casts cv::OutputArray to cv::Mat when executing. JavaCV ports OpenCV method only by using parameter interface and so it converts the parameter as std::vector, this results as a assertion failure.
It is required to convert std::vector to Mat to make it working. I don't know any other way exist for this conversion but otherwise it is possible to be fixed by only lib's author.
It is said that c++ version is working but in fact, it is working when pano parameter is given as cv::Mat, when std::vector is entered it gives the same failure assertions again.

Is there a way to force Magick++ to skip its cache when writing modified PixelPackets?

I have written a program that relies on Magick++ simply for importing and exporting of a wide variety of image formats. It uses Image.getPixels() to get a PixelPacket, does a lot of matrix transformations, then calls Image.syncPixels() before writing a new image. The general approach is the same as the example shown in Magick++'s documentation. More or less, the relevant code is:
Magick::Image image("image01.bmp");
image.modifyImage();
Magick::PixelPacket *imagePixels = image.getPixels(0, 0, 10, 10);
// Matrix manipulation occurs here.
// All actual changes to the PixelPacket direct changes to pixels like so:
imagePixels[i].red = 4; // or any other integer
// finally, after matrix manipulation is done
image.syncPixels();
image.write("image01_transformed.bmp");
When I run the above code, the new image file ("image01_transformed.bmp" in this example) ends up being the same as the original. However, if I write it to a different format, such as "image01_transformed.ppm", I get the correct result: a modified image. I assume this is due to a cached version of the format-encoded image, and that Magick++ is for some reason not aware that the image is actually changed and therefore the cache is out of date. I tested this idea by adding image.blur(1.0, 0.1); immediately before image.syncPixels();, and forcing this inconsequential change did indeed result in the correct result for same-format images.
Is there a way to force Magick++ to realize that the cache is out-of-date? Am I using getPixels() and syncPixels() incorrectly in the first place? Thanks!

Resources