How to construct the superpixel boundaries on the original image? - matlab

I performed a superpixel segmentation to a specific image. In my hand are the original RGB image and the labeled image contours. How can I draw the superpixels in the image?

The solution is written in the MATLAB documentation. The relevant part is:
Read image into the workspace.
A = imread('kobi.png');
Calculate superpixels of the image.
[L,N] = superpixels(A,500);
Display the superpixel boundaries overlaid on the original image.
figure
BW = boundarymask(L);
imshow(imoverlay(A,BW,'cyan'),'InitialMagnification',67)

Related

Region of interest extraction in MATLAB

I am writing a MATLAB code to implement a specific filter on a selected (from auto ROI) grayscale region of a forearm image which consists of veins. I also uploaded the forearm of a subject (after foreground has extracted).
Basically, I have NIR camera images of the forearm of different subjects with different orientations. I wrote the code that has extracted the foreground grayscale image of the arm, that gave me the white background with the forearm. I used Sobel edge to find edges. I also found the nonzero indices using the find function. I got the row and col indices. I need an idea on how to extract image inside (almost 10 pixels) of the edges detected on both sides of the forearm (black and white edged image-also uploaded).
Sobel-edge:
Foreground image:
ROI image that I need to extract:
clear all
close all
clc
image= rgb2gray(imread('Subj1.jpg'));
image1=~im2bw(image,0.1);
image1=im2uint8(image1);
foreground=imadd(image1,image);
imshow(foreground);
edgesmooth=medfilt2(foreground);
sobeledge= edge(edgesmooth,'sobel');
sobeledge=im2uint8(sobeledge);
figure
imshow(sobeledge);
[col,row]=find(sobeledge~=0);
Starting from the mask image that you make here:
image1=~im2bw(image,0.1);
but inverted, such that the mask is zero for the background and non-zero for the foreground:
image1 = im2bw(image,0.1);
you can use imdilate to expand it by a fixed distance:
se = strel('disk',20); % This will extend by 20/2=10 pixels
image2 = imdilate(image1,se);
image2 will be like image1, but expanded by 10 pixels in all directions.
imerode does the opposite, it shrinks regions.

how to remove the edge of an image after edge extraction using MATLAB?

I have an image in gray scale and I would like to remove the edge that is extracted by using an edge extraction method in MATLAB.
Is it possible to do it?
Below is my code that perform the edge extraction :
%load the image
A=imread('MikuBW.jpg');
%predefined edge extraction method in MATLAB
g=edge(A,'canny');
%plot results
subplot(2,1,1);
imshow(A),title('ORIGINAL');
subplot(2,1,2);
imshow(g),title('CANNY');
!(http://i.imgur.com/uS2Xxwf.jpg) [the result of original image and after edge extraction][1]
how do I remove the edge that is extracted in "CANNY" from the original image?
thank you for any help possible! =)
if you just want the "Canny" part to be removed, i.e. to be replaced with zeros, you can simply write:
A(g) = 0;
however, a more "natural" approach will be to blur the image, so it will have less edges in the first place:
H = fspecial('disk',5);
A = imfilter(A,H,'replicate');

face detection and make it blur

I am having trouble understanding the code about the Matlab
a = imread('Untitled2.png');
faceDetector = vision.CascadeObjectDetector;
bbox=step(faceDetector,a);
for j=1:size(bbox)
xbox=bbox(j,:);
subImage = imcrop(a, xbox);
H = fspecial('disk',10);
blurred = imfilter(subImage,H);
a(xbox(2):xbox(2)+xbox(4),xbox(1):xbox(1)+xbox(3),1:end) = blurred;
end
imshow(a);
Can anyone please kindly explain me that what is the for loop doing? I tried to use my own method to blur the face that I detected, but I just manage to crop out the face and blur the cropped image but I don't know how to put it back to original image. Then I tried to use the source code above I get from internet and the internet source code is able to blur the face and I can't understand the for loop logic. Kindly please explain to me, T^T.
Thanks.
As you can see there :
BBOX = step(detector,I) returns BBOX, an M-by-4 matrix defining M bounding boxes containing the detected objects. This method performs multiscale object detection on the input image, I. Each row of the output matrix, BBOX, contains a four-element vector, [x y width height], that specifies in pixels, the upper-left corner and size of a bounding box. The input image I, must be a grayscale or truecolor (RGB) image.
Are you sure that it's j=1:size(bbox) and not j=1:size(bbox,1) in the code?
Basically, The definition of BBox speaks by itself. The loop just iterates over all the boxes detected..
You then extract the informations about the jth box.
Then you extract the subImage given the position and size of xbox (xbox is a vector containing [x y width height]).
Then you define you filter.
Then you blur your subImage.
Then you override you image a with the blurred subimage using the informations in xbox.
EDIT : If you've already suceeded in blurring the cropped image, you just need to override you input image with your blurred image!

Creating intensity band across image border using matlab

I have this image (8 bit, pseudo-colored, gray-scale):
And I want to create an intensity band of a specific measure around it's border.
I tried erosion and other mathematical operations, including filtering to achieve the desired band but the actual image intensity changes as soon as I use erosion to cut part of the border.
My code so far looks like:
clear all
clc
x=imread('8-BIT COPY OF EGFP001.tif');
imshow(x);
y = imerode(x,strel('disk',2));
y1=imerode(y,strel('disk',7));
z=y-y1;
figure
z(z<30)=0
imshow(z)
The main problem I am encountering using this is that it somewhat changes the intensity of the original images as follows:
So my question is, how do I create such a band across image border without changing any other attribute of the original image?
Going with what beaker was talking about and what you would like done, I would personally convert your image into binary where false represents the background and true represents the foreground. When you're done, you then erode this image using a good structuring element that preserves the roundness of the contours of your objects (disk in your example).
The output of this would be the interior of the large object that is in the image. What you can do is use this mask and set these locations in the image to black so that you can preserve the outer band. As such, try doing something like this:
%// Read in image (directly from StackOverflow) and pseudo-colour the image
[im,map] = imread('http://i.stack.imgur.com/OxFwB.png');
out = ind2rgb(im, map);
%// Threshold the grayscale version
im_b = im > 10;
%// Create structuring element that removes border
se = strel('disk',7);
%// Erode thresholded image to get final mask
erode_b = imerode(im_b, se);
%// Duplicate mask in 3D
mask_3D = cat(3, erode_b, erode_b, erode_b);
%// Find indices that are true and black out result
final = out;
final(mask_3D) = 0;
figure;
imshow(final);
Let's go through the code slowly. The first two lines take your PNG image, which contains a grayscale image and a colour map and we read both of these into MATLAB. Next, we use ind2rgb to convert the image into its pseudo-coloured version. Once we do this, we use the grayscale image and threshold the image so that we capture all of the object pixels. I threshold the image with a value of 10 to escape some quantization noise that is seen in the image. This binary image is what we will operate on to determine those pixels we want to set to 0 to get the outer border.
Next, we declare a structuring element that is a disk of a radius of 7, then erode the mask. Once I'm done, I duplicate this mask in 3D so that it has the same number of channels as the pseudo-coloured image, then use the locations of the mask to set the values that are internal to the object to 0. The result would be the original image, but having the outer contours of all of the objects remain.
The result I get is:

LED Screen recognition in image using MATLAB

I'm trying to detect the screen border from the image (In need the 4 corners).
This is the Image:
I used HOUGH transform to detect lines and intersection points (the black circles) and this is the result:
Now I need to find the 4 corners or the 4 lines.. everything that will help me to crop the image, What can I do?
Maybe use the screen aspect ratio? but how?
I'm using Matlab.
Thanks.
A naive first approach that would do the trick if and only if you have same image conditions (background and laptop).
Convert your image to HSV (examine that in HSV the image inside the
screen is the only portion of the image with high Saturation, Value
values)
Create a mask by hard thresholding the Saturation and Value channels
Dilate the mask to connect disconnected regions
Compute the convex hull to get the mask boundaries
See a quick result:
Here is the same mask with the original image portion that makes it through the mask:
Here is the code to do so:
img = imread( 'imagename.jpg'); % change the image name
hsv = rgb2hsv( img);
mask = hsv(:,:,2)>0.25 & hsv(:,:,3)>0.5;
strel_size = round(0.025*max(size(mask)));
dilated_mask=imdilate(mask,strel('square',strel_size));
s=regionprops(dilated_mask,'BoundingBox','ConvexHull');
% here Bounding box produces a box with the minimum-maximum white pixel positions but the image is not actually rectangular due to perspective...
imshow(uint8(img.*repmat(dilated_mask,[1 1 3])));
line(s.ConvexHull(:,1),s.ConvexHull(:,2),'Color','red','LineWidth',3);
You may, of course, apply some more sophisticated processing to be a lot more accurate and to correct the convex hull to be just a rectangular shape with perspective, but this is just a 5 minutes attempt just to showcase the approach...