I'm doing a watershed segmentation and the marker image is derived from the source image put through a distance transform. The distance transform returns a floating point image (I have no idea about the bit-depth) and I have trouble putting it through the watershed method since it requires a 32 bit single channel image.
Can I use the mat's convertTo method to set the bit depth to 32?
I also have trouble trying to display the floating point image since the matToBitmap() method doesn't seem to accept them. (in Android)
Mat mImg = new Mat();
Mat mThresh = new Mat();
Mat mDist = new Mat();
ImageView imgView = (ImageView) findViewById(R.id.imageView);
Bitmap bmpIn = BitmapFactory.decodeResource(getResources(),
R.drawable.w1);
Utils.bitmapToMat(bmpIn, mImg);
Imgproc.cvtColor(mImg, mImg, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(mImg, mThresh, 0, 255, Imgproc.THRESH_BINARY
| Imgproc.THRESH_OTSU);
//Marker image for watershed
Imgproc.distanceTransform(mThresh, mDist, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_PRECISE);
//Conversions for watershed
Imgproc.cvtColor(mThresh, mThresh, Imgproc.COLOR_GRAY2BGR, 3);
//Floating-point image -> 32-bit single-channel
mDist.convertTo(...);
Imgproc.watershed(mThresh, mDist); //
Bitmap bmpOut = Bitmap.createBitmap(mThresh.cols(), mThresh.rows(),
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mThresh, bmpOut);
imgView.setImageBitmap(bmpOut);
Yes, you can use the convertTo function to convert any opencv matrix to another type. The type to convert to should be set in a destination matrix with the same size. convertTo has optional parameters scale and shift, so you can avoid clipping and quantization errors when converting to fixed point depths. So for your code:
Mat mDist32 = Mat(mDist.rows,mDist.cols,CV_32SC1); // 32 bit signed 1 channel, use CV_32UC1 for unsigned
mDist.convertTo(mDist32,CV_32SC1,1,0);
Imgproc.watershed(mThresh,mDist32);
Related
I am using OpenCV android library thresholding method for image segmentation, but the problem is that the output bitmap contains black background which I do not want please note that original image does not have any black background it is actually white. I am attaching the code for your reference, I am new to opencv and don't have much understanding about it also so kindly help me out.
private void Segmentation() {
Mat srcMat = new Mat();
gray = new Mat();
Utils.bitmapToMat(imageBmp, srcMat);
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
grayBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(gray, grayBmp);
grayscaleHistogram();
Mat threshold = new Mat();
Imgproc.threshold(gray, threshold, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
thresBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(threshold, thresBmp);
Mat closing = new Mat();
Mat kernel = Mat.ones(5, 5, CvType.CV_8U);
Imgproc.morphologyEx(threshold, closing, Imgproc.MORPH_CLOSE, kernel, new Point(-1, -1), 3);
closingBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(closing, closingBmp);
result = new Mat();
Core.subtract(closing, gray, result);
Core.subtract(closing, result, result);
resultBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(result, resultBmp);
Glide.with(ResultActivity.this).asBitmap().load(resultBmp).into(ivAfter);
}
enter image description here
What exactly do you want it to be then? Binary thresholding works like this:
if value < threshold:
value = 0
else:
value = 1
Of course you can convert it to a grayscale / RGB image and adjust the background to your liking. You can also invert your image (white background, black segmentation) by using the ~ operator.
segmented_image = ~ segmented_image
Edit: OpenCV has a dedicated flag to invert the results: CV_THRESH_BINARY_INV You are already using it, maybe try changing it to CV_THRESH_BINARY
I get a mat (320x480) from the camera of the phone. Then I need to process a part of that frame. I use ROI for that:
mat2 = new Mat(width, 175, CvType.CV_8SC3);
Rect roi = new Rect(75, 0, 175, 320);
mat2 = new Mat(mat1, roi);
Now I want to create a new mat with dimensions 320x480 with a black background. How do I do that?
Then I want to copy the processed ROI to the new mat at the same place it was on the first mat. How do I do that?
I am using OpenCV 3.4.6 and android studio.
Thank you in advance
You just need to submat() an ROI from the original Mat (your camera frame), and then process the submat as a normal Mat, but be careful to do not clone the sumbmat if you want the original get the effect.
Update according to the comment:
Mat originalMat = someMat;
Mat blackMat = Mat.zeros(size, CvType.CV_8UC1); // it is your black mat
// create a submat from the original mat, and clone it
Mat roiMat = originalMat.submat(rect).clone();
..... // do what you want with roiMat
// now copy the result to the original mat
Mat dst = originalMat.submat(rect); // do not clone this submat
roiMat.copyTo(dst);
// done!
I am creating a project, where I have to remove the background from the image and detect the object.
I am using canny edge detection for detecting edges and than finding contours and than draw contours on a masked image, but after canny edge detection, I am getting broken edges ,how to fix that.
For Canny edge detection, for Threshold parameter, I have tried using thresholding with otsu's method for higher and lower threshold, but it doesn't seem to give appropriate result. Further, I have tried finding the mean of pixel values, and finding
double high_threshold = 1.33 * d;
double low_threshold = 0.66 * d;
it is also not giving accurate result. what else I can do
Mat rgba = new Mat();
Utils.bitmapToMat(bitmap, rgba);
Mat edges = new Mat(rgba.size(), CvType.CV_8UC1);Imgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.GaussianBlur(edges, edges, new Size(3,3), 2); Mat thresh=new Mat();
double upper_threshold = Imgproc.threshold(edges,thresh,0,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C| Imgproc.THRESH_OTSU);
double lower_threshold = 0.1*upper_threshold;Imgproc.Canny(edges,edges,upper_threshold,lower_threshold,3,false);Mat mDilatedMat = new Mat();
Mat Meroded = new Mat();
double erosion_size=5;
double dilation_size=4;
Mat e= Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*erosion_size + 1, 2*erosion_size+1));
Mat f= Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*dilation_size + 1, 2*dilation_size+1));
Imgproc.dilate(edges, mDilatedMat,e);
Imgproc.erode(mDilatedMat, Meroded,f);
You can improve your image extracted by sobel , canny or a different algorithm by applying edge linking algorithm.
Many edge linking algorithms are avaliable to use such as hough transform,
ant colony algorithm etc.
I am trying to plot the R channel of an image as a heatmap using the following code:
// Read image as a Mat of 32bit float
Mat imgMain = new Mat(n_height, n_width, CvType.CV_32FC4);
image = BitmapFactory.decodeFile("img.jpg");
Utils.bitmapToMat(image, imgMain);
imgMain.convertTo(imgMain, CvType.CV_32FC4);
// Extract R channel
Mat imgChR = new Mat(n_height, n_width, CvType.CV_32FC1);
extractChannel(imgMain, imgChR, 0);
// imgChR processed with floating point arithmetic
// Convert to HeatMap
normalize(imgChR, imgChR, 0, 1, NORM_MINMAX);
Mat heatOut = new Mat(n_height, n_width, CvType.CV_8UC4);
applyColorMap(imgChR, heatOut, COLORMAP_JET);
// Save as Image
pimage = Bitmap.createBitmap(n_width, n_height, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(heatOut, pimage);
File f = new File("heat.jpeg");
FileOutputStream fos = new FileOutputStream(f);
pimage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
For averaging multiple copies of the image, I am converting the matrix from integers to floats. (The averaging process is abstracted in the following code but please note that the type has to be converted to a 32bit float)
Using the following code, I simply get a black image.
Is this the right approach for plotting a heatmap of a single channel matrix?
If not, is there any other function or approach that can be used?
(When I check the type of heatOut the function returns a 0 as a integer representation of the Mat type.)
i have code to load image from sdcard and post it to ImageView.
Mat mRgba = Highgui.imread(dir);
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mRgba, bmp);
mImage.setImageBitmap(bmp, true, null, 5.0f);
the image is loaded but it's wrong color. Color seem to be inverted (but not inverted).
Here is image comparison
I tried to load image by
Bitmap bmp = BitmapFactory.decodeFile(dir);
It worked correctly. But i have to use Highgui.imread.
What wrong with my code?
You will have to use something like this:
Mat inputImage = Highgui.imread(pathToFile);
Mat tmp = new Mat();
Imgproc.cvtColor(inputImage, tmp, Imgproc.COLOR_BGR2RGB);
Bitmap imageToShow = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, imageToShow);
You're trying to load a bitmap supposing that the image is 8-bit/color RGBA: are you sure of that?
Also note that ARGB is not RGBA. You may need to re-arrange the bytes of each pixel. Something like
int pixel = get_the_pixel();
int alpha = 0xff & pixel;
pixel = pixel<<8 | alpha;
set_the_pixel(pixel);
You'll want to do something more efficient than accessor methods shown here, but you get the idea.