how to divide the matlab image into 3 sections or boxs??

i have resolution of 1280*720 i need to divide 720 into 3 partition and keep 1280 as is it ,how can i do it??and where do i add code in the below program?ie divide into 1280*240 1280*240 and 1280*240
Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput('winvideo', 1, 'I420_1280x720');
% Set the properties of the video object set(vid, 'FramesPerTrigger', Inf); set(vid, 'ReturnedColorspace', 'rgb') vid.FrameGrabInterval = 5;
%start the video aquisition here start(vid) % Set a loop that stop after 100 frames of aquisition while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition. stop(vid);
% Flush all the image data stored in the memory buffer. flushdata(vid);
% Clear all variables clear all

 Accepted Answer

Try this:
dataTop = rgb2gray(data(1:240, :,:));
dataMiddle = rgb2gray(data(241:480, :, :));
dataBottom = rgb2gray(data(481:end, :, :));

11 Comments

thank u .but,how do i know the above one is working for my program,and plz can u tell me where to copy it in above code and do i need put loops for getting output separately from each data section.u have divided 720 in above codes so it means 1280 remains undivided no??
I did not see any place in your code where it might make sense to divide a frame vertically into 3 panels. Correct, the 1280 is not divided, just like you required when you said "keep 1280 as is it". If the 720 is columns, not rows, then the code would be:
dataTop = rgb2gray(data(:, 1:240,:));
dataMiddle = rgb2gray(data(:, 241:480, :));
dataBottom = rgb2gray(data(:, 481:end, :));
This should divide it into 3 panels in the horizontal direction.
thank you,but color should not be changed to grey image,i need to divide into three columns *horizontally*so that each column can be controlled individually,that is why i divided 720 into three,can u edit the program i have written such that it has to detect red color(tomato) in each column(all 3 column will have tomato at same time),if it detects red color in any 1,2,3 column or combinations it should pass a signal separately to each aurdino pin actually..
can i divide using "for object = 1:length(stats)" setting regions?
Then don't use rgb2gray() and it will stay as a color image.
and code to check if any red object is present between these data blocks??
what is code to send a signal to ardino uno pin (ardino pin is connected to servo motor),when in the below program if tomato1 is absent and i need to run motor1 in that case.?plz help for i = 1:object if stats(i).Centroid(2) > 0 && stats(i).Centroid(2) < 160 sprintf('Tomato 1 is there') else sprintf('Tomato 1 is absent') end end for i = 1:object if stats(i).Centroid(2) > 160 && stats(i).Centroid(2) < 320 sprintf('Tomato 2 is there') else sprintf('Tomato 2 is absent') end end for i = 1:object if stats(i).Centroid(2) > 320 && stats(i).Centroid(2) < 480 sprintf('Tomato 3 is there') else sprintf('Tomato 3 is absent') end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!