You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Handling text files and viewing annotations on a Picture.
2 views (last 30 days)
Show older comments
I have to import a text file in Matlab that contains several random numbers representing X and Y co-ordinates. Then I have to show these points on a .png picture. The text file contains 20 such points. I used "dlmread" but it makes a single matrix of all these points while I need 20 such matrices. How can I make several Matrices using that text file and how can I show these points in the Picture.?
P.S The points are like:
1428 742
256 84
986 547... etc
These points represent the pixels of a picture and I have to show them in a different color, its like darts hitting the picture but I have no idea how to do it.
Accepted Answer
Image Analyst
on 15 Jan 2015
I fail to see why you need 20 matrices when all your x,y data is in the one matrix you get from dlmread(). How about just calling dlmread() and plot(), something like this:
data = dlmread(filename, ' ');
[rows, columns] = size(data);
imshow(yourImage);
hold on;
for row = 1 : rows
randomColor = rand(1,3);
plot(data(row, 1), data(row, 2), '.', ...
'Color', randomColor, 'MarkerSize', 3);
end
20 Comments
Saad Saboor
on 15 Jan 2015
Edited: Saad Saboor
on 15 Jan 2015
Thanks, that helped. Actually, Im new to Matlab and I have an assignment to complete. There's one more thing.
There are five faces in the picture. I have to make annotated rectangles over them. For this, I have a text file containing the dimensions of the rectangles. How can I use this text file to draw rectangles? And after that, when the points will hit the picture, I have to change the color of the point hitting the face as well as the rectangle surrounding that face and I have no idea how to do it.
Image Analyst
on 15 Jan 2015
You can use rectangle() or plot() to display boxes in the overlay above the picture. All points in the box will be in the picture, at least I've not tried making a box that leaves the picture (axes). If you want the box to have a different color when it's over some particular part of the image, then you'll have to use plot() or line() to draw line segments over that part in the color that you want.
line([x1 x2], [y1, y2], 'Color', 'b', 'LineWidth', 3);
or something like that. But you have to know in advance where the parts of that box are that need to be in a different color.
Saad Saboor
on 15 Jan 2015
Edited: Saad Saboor
on 15 Jan 2015
Image=imread('avengers.png');
Data1 = dlmread('Annotations.txt');
[rows, columns] = size(Data1);
Data2 = dlmread('randPts1.txt');
[rows1, columns1] = size(Data2);
imshow(Image);
hold on;
for row = 1 : rows;
rectangle('Position',[Data1(row,1),Data1(row,2),Data1(row,3),...
Data1(row,4)],'Curvature',[0,0],'LineWidth',2, 'EdgeColor','r');
end
for row1 = 1 : rows1;
plot(Data2(row1,1), Data2(row1,2), '.', ...
'Color', 'blue', 'MarkerSize', 15);
end
I have used this code to plot blue points on the picture as you can see. The only thing that still needs to be done is to change the color of those points that are lying inside the rectangle as well as the the rectangle itself. I tried to develop a nested if statement to achieve this but it didnt worked. I also dont know how to use inpolygon to find these points and change their color.
Can you help me write a general code for that since I have to perform this 10 times with different points.?
Image Analyst
on 15 Jan 2015
In the first loop:
x1 = Data1(row,1);
y1 = Data1(row,2)
x2 = x1 + Data1(row,3);
y2 = y1 + Data1(row,4);
xRect(row, 1:5) = [x1,x2,x2,x1,x1];
yRect(row, 1:5) = [y1,y1,y2,y2,y1];
Inside the second loop
x = Data2(row1,1);
y = Data2(row1,2);
inside = false
% See if the x,y is inside any of the rectangles.
for r = 1 : rows
inside = inpolygon(x, y, xRect(r,:),yRect(r,:))
if inside
break;
end
end
if inside
% It's inside one of the rectangles, so plot it in red.
plot(x, y, 'Color', 'r*', 'MarkerSize', 15);
else
% It's not inside any rectangle, so plot it in blue.
plot(x, y, 'Color', 'b*', 'MarkerSize', 15);
end
Saad Saboor
on 16 Jan 2015
Thanks very very much, now I've got those points with a different color, but still I need to get those rectangles in a different color too.
rectangle('Position',[Data1(row,1),Data1(row,2),Data1(row,3),...
Data1(row,4)],'Curvature',[0,0],'LineWidth',2,...
'Linestyle','--', 'EdgeColor','yellow');
Where should I put this code or what should I do to change only those rectangles which contain the points?
Image Analyst
on 16 Jan 2015
Just write over the other, earlier rectangles. Inside the "if" just draw the new rectangle in the other color.
if inside
% It's inside one of the rectangles, so plot it in red.
plot(x, y, 'Color', 'r*', 'MarkerSize', 15);
rectangle('Position',[Data1(row,1),Data1(row,2),Data1(row,3),...
Data1(row,4)],'Curvature',[0,0],'LineWidth',2,...
'Linestyle','--', 'EdgeColor','yellow');
else
Saad Saboor
on 16 Jan 2015
I've tried this before but it didnt helped. Here's my code:
Image=imread('avengers.png');
Data1 = dlmread('Annotations.txt');
[rows, columns] = size(Data1);
Data2 = dlmread('randPts1.txt');
[rows1, columns1] = size(Data2);
imshow(Image);
hold on;
xRect(row, 1:5)=0;
yRect(row, 1:5)=0;
for row = 1 : rows;
x1 = Data1(row,1);
y1 = Data1(row,2);
x2 = x1 + Data1(row,3);
y2 = y1 + Data1(row,4);
xRect(row, 1:5) = [x1,x2,x2,x1,x1];
yRect(row, 1:5) = [y1,y1,y2,y2,y1];
rectangle('Position',[Data1(row,1),Data1(row,2),Data1(row,3),...
Data1(row,4)],'Curvature',[0,0],'LineWidth',2, 'EdgeColor','r');
end
for row1 = 1 : rows1;
x = Data2(row1,1);
y = Data2(row1,2);
inside = false;
% See if the x,y is inside any of the rectangles.
for r = 1 : rows
inside = inpolygon(x, y, xRect(r,:),yRect(r,:));
if inside
break;
end
end
if inside
% It's inside one of the rectangles, so plot it in red.
plot(x, y, '.', 'Color', 'green', 'MarkerSize', 15);
else
% It's not inside any rectangle, so plot it in blue.
plot(x, y, '.', 'Color', 'blue', 'MarkerSize', 15);
end
end
Please tell me where to put it exactly.?
Image Analyst
on 17 Jan 2015
Saad, here is the code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Read in image.
Image=imread('avengers.png');
% Read in rectangles.
rectangles = dlmread('Annotations.txt');
[rows, columns] = size(rectangles);
% Read in random points
randomPoints = dlmread('randPts1.txt');
[rows1, columns1] = size(randomPoints);
% Display image
imshow(Image);
hold on;
axis on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Plot rectangles over image.
xRect = zeros(rows, 5); % Preallocate.
yRect = zeros(rows, 5); % Preallocate.
for row = 1 : rows;
x1 = rectangles(row,1);
y1 = rectangles(row,2);
x2 = x1 + rectangles(row,3);
y2 = y1 + rectangles(row,4);
xRect(row, 1:5) = [x1,x2,x2,x1,x1];
yRect(row, 1:5) = [y1,y1,y2,y2,y1];
rectangle('Position',[rectangles(row,1),rectangles(row,2),rectangles(row,3),...
rectangles(row,4)],'Curvature',[0,0],'LineWidth',2, 'EdgeColor','r');
end
% uiwait(msgbox('These are the rectangles'));
% Plot the markers over the image.
for row1 = 1 : rows1;
x = randomPoints(row1,1);
y = randomPoints(row1,2);
inside = false;
% See if the x,y is inside any of the rectangles.
for r = 1 : rows
inside = inpolygon(x, y, xRect(r,:),yRect(r,:));
if inside
break;
end
end
if inside
% It's inside one of the rectangles, so plot it in green.
plot(x, y, '.', 'Color', 'g', 'MarkerSize', 35);
label = sprintf('Point #%d', row1);
text(x+10, y, label, 'FontSize', fontSize, 'Color', 'g');
% Replot the rectangle in green because it contains a random point.
% "r" is the rectangle number that it is inside.
rectangle('Position', ...
[rectangles(r,1),rectangles(r,2),rectangles(r,3),...
rectangles(r,4)], 'Curvature',[0,0], ...
'LineWidth', 2, 'EdgeColor', 'g');
else
% It's not inside any rectangle, so plot it in blue.
plot(x, y, '.', 'Color', 'b', 'MarkerSize', 35);
label = sprintf('Point #%d', row1);
text(x+10, y, label, 'FontSize', fontSize, 'Color', 'b');
end
end
Saad Saboor
on 17 Jan 2015
Edited: Saad Saboor
on 17 Jan 2015
Thank you very very very Much.....!!! I would've have kissed your cheeks if I were there...!!!!!
Saad Saboor
on 17 Jan 2015
Is there any way to count the Hit-Ratio, i.e the percentage of points hitting the face out of all the points..?
Image Analyst
on 17 Jan 2015
Just make inside an array, and then set it to true if it's inside, then sum the array after you're done with the loop
inside(row1) = false;
and so on everywhere you refer to inside. Then after the loop:
percentInside = sum(inside)/numel(inside) * 100;
fawad khan
on 22 Jan 2015
What is 'yourImage' in your answer? Is it the filename of the image file you used in dlmread()??
Image Analyst
on 22 Jan 2015
You have an image that you're displaying these points on don't you? You said, "These points represent the pixels of a picture". Well yourImage is the name of your picture variable. What did you call it?
Saad Saboor
on 23 Jan 2015
Sir, I've used a for loop to run the code 20 times, every time with different random points. In each iteration, I calculate hit-ratio but when the next loop starts, it overwrites the previous value stored. How can I keep those 20 hit-ratios saved till the loop ends and display them as a Bar Graph?
Image Analyst
on 24 Jan 2015
In the outer loop - let's say the loop index is called experiment - just index the percentInside:
percentInside(experiment) = sum(inside)/numel(inside) * 100;
Saad Saboor
on 30 Jan 2015
Hey, one more thing, inside=inpolygon includes both the points inside and those on the line, but I have to count only those inside, what should i change in the code to ignore the points lying on the rectangle boundary?
Image Analyst
on 30 Jan 2015
I'm sure you must have thought of the obvious idea to adjust xRect and yRect so that the rectangle is one pixel smaller:
xRect(row, 1:5) = [x1+1,x2-1,x2-1,x1+1,x1+1];
yRect(row, 1:5) = [y1+1,y1+1,y2-1,y2-1,y1+1];
Was there some reason why it didn't work?
Saad Saboor
on 2 Feb 2015
No, the code's perfect, I just had to plot the points on the rectangle boundary as blue, so thanks, it's working all fine.
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)