Yes, It is possible to access and call Python from Matlab. Similarly, access of Matlab from Python also possible. Below is the video tutorial by Mathworks.
Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts
Finding an object in Image and Counting of an object

clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
tic;
% Read image
original = imread('F04U2MPIXQG3WQS.LARGE.jpeg');
figure,imshow(original)
title('Original Image','FontSize',fontSize);
% RGB component
R=original(:,:,1);
G=original(:,:,2);
B=original(:,:,3);
figure,imshow(R)
title('Red Component Image','FontSize',fontSize);
figure,imshow(G)
title('Green Component Image','FontSize',fontSize);
figure,imshow(B)
title('Blue Component Image','FontSize',fontSize);
% Thresholding
% Select Red compoent
level=graythresh(R);
binary=im2bw(R,level);
figure,imshow(binary)
title('Thresholed Image','FontSize',fontSize);
% Use Morphological Operations
erosion=imerode(binary,strel('disk',2));
figure,imshow(erosion)
title('Eroded Image','FontSize',fontSize);
% Use Watershed segmentation
D = bwdist(erosion);
figure
imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw')
D = -D;
D(~erosion) = -Inf;
L = watershed(D);
Lrgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(Lrgb,'InitialMagnification','fit')
title('Watershed transform of D')
figure,imshow(imfuse(erosion,Lrgb))
axis([])
bw2 = ~bwareaopen(~erosion, 10);
imshow(bw2)
D = -bwdist(~erosion);
figure,imshow(D,[])
Ld = watershed(~D);
figure,imshow(label2rgb(Ld))
bw2 = erosion;
bw2(Ld == 0) = 0;
mask = imextendedmin(D,2);
D2 = imimposemin(D,mask);
Ld2 = watershed(D2);
bw3 = erosion;
bw3(Ld2 == 0) = 0;
figure,imshow(erosion);
title('Watershed Segmentation','FontSize',fontSize)
% labelling
figure,imshow(original);
title('Labelling','FontSize',fontSize)
s=regionprops(logical(bw3),'Centroid','Area','Eccentricity','Perimeter');
hold on
for k=1:numel(s)
c=s(k).Centroid;
text(c(1),c(2),sprintf('%d',k),...
'HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
hold off
Num = numel(s);
message= sprintf('You have counted %i Strawberries',Num);
h=msgbox(message);
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
tic;
% Read image
original = imread('F04U2MPIXQG3WQS.LARGE.jpeg');
figure,imshow(original)
title('Original Image','FontSize',fontSize);
% RGB component
R=original(:,:,1);
G=original(:,:,2);
B=original(:,:,3);
figure,imshow(R)
title('Red Component Image','FontSize',fontSize);
figure,imshow(G)
title('Green Component Image','FontSize',fontSize);
figure,imshow(B)
title('Blue Component Image','FontSize',fontSize);
% Thresholding
% Select Red compoent
level=graythresh(R);
binary=im2bw(R,level);
figure,imshow(binary)
title('Thresholed Image','FontSize',fontSize);
% Use Morphological Operations
erosion=imerode(binary,strel('disk',2));
figure,imshow(erosion)
title('Eroded Image','FontSize',fontSize);
% Use Watershed segmentation
D = bwdist(erosion);
figure
imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw')
D = -D;
D(~erosion) = -Inf;
L = watershed(D);
Lrgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(Lrgb,'InitialMagnification','fit')
title('Watershed transform of D')
figure,imshow(imfuse(erosion,Lrgb))
axis([])
bw2 = ~bwareaopen(~erosion, 10);
imshow(bw2)
D = -bwdist(~erosion);
figure,imshow(D,[])
Ld = watershed(~D);
figure,imshow(label2rgb(Ld))
bw2 = erosion;
bw2(Ld == 0) = 0;
mask = imextendedmin(D,2);
D2 = imimposemin(D,mask);
Ld2 = watershed(D2);
bw3 = erosion;
bw3(Ld2 == 0) = 0;
figure,imshow(erosion);
title('Watershed Segmentation','FontSize',fontSize)
% labelling
figure,imshow(original);
title('Labelling','FontSize',fontSize)
s=regionprops(logical(bw3),'Centroid','Area','Eccentricity','Perimeter');
hold on
for k=1:numel(s)
c=s(k).Centroid;
text(c(1),c(2),sprintf('%d',k),...
'HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
hold off
Num = numel(s);
message= sprintf('You have counted %i Strawberries',Num);
h=msgbox(message);
Boundary an Object in Image using MATLAB
Step 1: Read the Image

An image is choosen so that it has considerable contrast with its background so that the objects can be identified. The function imread reads the image from a given location and stores it in the matrix img1. The imshow function displays the image.
img1=imread('Lines.jpg');
imshow(img1)
Step 2: Convert the Image to Grayscale

This step removes any color information in the image to make it
easier to identify objects. The rgb2gray converts the color image into a
grayscale image and stores it into the matrix img2.
img1=rgb2gray(img1);
imshow(img1)
img1=rgb2gray(img1);
imshow(img1)
Step 3: Threshold the Image

This step thresholds the image by converting the grayscale image into an image that contains only two colors. The function im2bw() assigns black color to all the pixels that have luminosity than a threshold level and the others as white. the function graythresh() approximately calculates the threshold of the image.
img2=im2bw(img1,graythresh(img1));
imshow(img2)
Step 4: Complement the Image

In this step we complement the image by using the ~ operator. By
this we convert the white patches into black and vice versa. We perform
this step as we wanted the areas of concern(objects) to be colored
white.
img2=~img2;
imshow(img2)
img2=~img2;
imshow(img2)
Step 5: Find the Boundaries of the Objects

This step finds the boundaries of each object that it finds and
stores it in B. The text function prints the number of objects that are
found by bwboundaries.
B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on
B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on
Step 6: Draw the Boundaries

This is a fun step in which we mark the boundaries of all the objects identified by bwboundaries function. This step can be eliminated if it seems complicated
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
end
img1=imread('Lines.jpg');
imshow(img1)
img1=rgb2gray(img1);
imshow(img1)
img2=im2bw(img1,graythresh(img1));
imshow(img2)
img2=~img2;
imshow(img2)
B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
end
Object Detection in a Cluttered Scene Using Point Feature Matching
This example shows how to detect a particular object in a cluttered scene, given a reference image of the object.
Overview
This example presents an algorithm for detecting a specific object
based on finding point correspondences between the reference and the
target image. It can detect objects despite a scale change or in-plane
rotation. It is also robust to small amount of out-of-plane rotation and
occlusion.
This method of object detection works best for objects that exhibit
non-repeating texture patterns, which give rise to unique feature
matches. This technique is not likely to work well for uniformly-colored
objects, or for objects containing repeating patterns. Note that this
algorithm is designed for detecting a specific object, for example, the
elephant in the reference image, rather than any elephant. For detecting
objects of a particular category, such as people or faces, see vision.PeopleDetector and vision.CascadeObjectDetector.
Step 1: Read Images
Read the reference image containing the object of interest.
boxImage = imread('stapleRemover.jpg'); figure; imshow(boxImage); title('Image of a Box');

Read the target image containing a cluttered scene.
sceneImage = imread('clutteredDesk.jpg'); figure; imshow(sceneImage); title('Image of a Cluttered Scene');

Step 2: Detect Feature Points
Detect feature points in both images.
boxPoints = detectSURFFeatures(boxImage); scenePoints = detectSURFFeatures(sceneImage);
Visualize the strongest feature points found in the reference image.
figure; imshow(boxImage); title('100 Strongest Feature Points from Box Image'); hold on; plot(selectStrongest(boxPoints, 100));

Visualize the strongest feature points found in the target image.
figure; imshow(sceneImage); title('300 Strongest Feature Points from Scene Image'); hold on; plot(selectStrongest(scenePoints, 300));

Step 3: Extract Feature Descriptors
Extract feature descriptors at the interest points in both images.
[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints); [sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);
Step 4: Find Putative Point Matches
Match the features using their descriptors.
boxPairs = matchFeatures(boxFeatures, sceneFeatures);
Display putatively matched features.
matchedBoxPoints = boxPoints(boxPairs(:, 1), :); matchedScenePoints = scenePoints(boxPairs(:, 2), :); figure; showMatchedFeatures(boxImage, sceneImage, matchedBoxPoints, ... matchedScenePoints, 'montage'); title('Putatively Matched Points (Including Outliers)');

Step 5: Locate the Object in the Scene Using Putative Matches
estimateGeometricTransform calculates the transformation
relating the matched points, while eliminating outliers. This
transformation allows us to localize the object in the scene.
[tform, inlierBoxPoints, inlierScenePoints] = ... estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, 'affine');
Display the matching point pairs with the outliers removed
figure; showMatchedFeatures(boxImage, sceneImage, inlierBoxPoints, ... inlierScenePoints, 'montage'); title('Matched Points (Inliers Only)');

Get the bounding polygon of the reference image.
boxPolygon = [1, 1;... % top-left size(boxImage, 2), 1;... % top-right size(boxImage, 2), size(boxImage, 1);... % bottom-right 1, size(boxImage, 1);... % bottom-left 1, 1]; % top-left again to close the polygon
Transform the polygon into the coordinate system of the target image.
The transformed polygon indicates the location of the object in the
scene.
newBoxPolygon = transformPointsForward(tform, boxPolygon);
Display the detected object.
figure; imshow(sceneImage); hold on; line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y'); title('Detected Box');

Step 7: Detect Another Object
Detect a second object by using the same steps as before.
Read an image containing the second object of interest.
elephantImage = imread('elephant.jpg'); figure; imshow(elephantImage); title('Image of an Elephant');

Detect and visualize feature points.
elephantPoints = detectSURFFeatures(elephantImage); figure; imshow(elephantImage); hold on; plot(selectStrongest(elephantPoints, 100)); title('100 Strongest Feature Points from Elephant Image');

Extract feature descriptors.
[elephantFeatures, elephantPoints] = extractFeatures(elephantImage, elephantPoints);
Match Features
elephantPairs = matchFeatures(elephantFeatures, sceneFeatures, 'MaxRatio', 0.9);
Display putatively matched features.
matchedElephantPoints = elephantPoints(elephantPairs(:, 1), :); matchedScenePoints = scenePoints(elephantPairs(:, 2), :); figure; showMatchedFeatures(elephantImage, sceneImage, matchedElephantPoints, ... matchedScenePoints, 'montage'); title('Putatively Matched Points (Including Outliers)');

Estimate Geometric Transformation and Eliminate Outliers
[tform, inlierElephantPoints, inlierScenePoints] = ... estimateGeometricTransform(matchedElephantPoints, matchedScenePoints, 'affine'); figure; showMatchedFeatures(elephantImage, sceneImage, inlierElephantPoints, ... inlierScenePoints, 'montage'); title('Matched Points (Inliers Only)');

Display Both Objects
elephantPolygon = [1, 1;... % top-left size(elephantImage, 2), 1;... % top-right size(elephantImage, 2), size(elephantImage, 1);... % bottom-right 1, size(elephantImage, 1);... % bottom-left 1,1]; % top-left again to close the polygon newElephantPolygon = transformPointsForward(tform, elephantPolygon); figure; imshow(sceneImage); hold on; line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y'); line(newElephantPolygon(:, 1), newElephantPolygon(:, 2), 'Color', 'g'); title('Detected Elephant and Box');

Ref : Maltab Programming examples
Subscribe to:
Posts (Atom)