How can I create a sliding window for detect orange ball photo with bounding box?

3 views (last 30 days)
Hello, I just want to create a program for detecting orange ball photo with bounding box like this.
I want to create program that detect the orange ball automatically with sliding window method, but I don't know how to do it. Can somebody instuct me how to do it? Here is my original manual code. Thank you for your help in advance!
%% Load the image
img = imread("orangeball.jpg");
% display img size
%% obtain the top-left pixel and bottom-right pixels from plot pane
% using data tips cursor menu (top-right of image)
x_1 = 217.5; % top-left column
y_1 = 200.5; % top-left row
x_2 = 316.5; % bot-right col
y_2 = 285; % bot-right row
%% plot a rectangle box as an overlay
% hold the current figure
hold on;
% MATLAB convension
% please use command "help rectangle" for docs
% rectangle('Position', [top-left x, top-left y, width, height]);
rectangle('Position',[x_1, y_1, x_2-x_1, y_2-y_1],'EdgeColor','y');
  1 Comment
Adam Danz
Adam Danz on 12 Oct 2022
> I want to create program that detect the orange ball automatically with sliding window method, but I don't know how to do it. Can somebody instuct me how to do it?
Welcome to the forum, @Krittanai. The Answers forum is a great place to get un-stuck when solving problems using MATLAB but your question has a much wider scope. Your question is about how to do an entire project and you seems to be in the learning and discovering phase. After you learn more about object detection using sliding windows and when you start to implement this in MATLAB, this is the best place to ask specific questions.
In the mean time, there are books, articles, blogs, and other forums that might be helpful. For example, I found this link below after search for less time than it took me to write this :)
As for your code, you are reading in the image correctly and displaying the rectangle correctly but remember that you don't need to display the image to do computations on the image. However, you'll definitely want to plot the image and the rectangle at then end after your algorithm identifies the object.

Sign in to comment.

Answers (1)

Amir Azadeh Ranjbar
Amir Azadeh Ranjbar on 12 Oct 2022
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 12-Oct-2022
% Convert RGB image to chosen color space
I = rgb2ycbcr(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 58.000;
channel1Max = 176.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 58.000;
channel2Max = 105.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 170.000;
channel3Max = 217.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
SE = strel('Disk',30,4);
morphed = imdilate(BW, SE);
BBox = regionprops(morphed,"BoundingBox") ;
im = insertObjectAnnotation(RGB,"rectangle",BBox.BoundingBox,"Ball");

Sign in to comment.


Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by