This is the link to the dataset: https://data.caltech.edu/records/mzrjq-6wc02.
The dataset has images and respective contours in .mat
files.
How to plot the contour in Python?
They have code that shows plotting the contour but it’s written in matlab:
function show_annotation(imgfile, annotation_file);
%%
%% imgfile: string
%% annotation_file: string
%%
%% written by by Fei-Fei Li - November 2004
%%
IMTYPE = 'jpg';
GUIDELINE_MODE = 1;
%% Parameters
%label_abbrev = {'LE', 'RE', 'LN', 'NB', 'RN', 'LM', 'RM'};
LARGEFONT = 28;
MEDFONT = 18;
BIG_WINDOW = get(0,'ScreenSize');
SMALL_WINDOW = [100 100 512 480];
%% load the annotated data
load(annotation_file, 'box_coord', 'obj_contour');
%% Read and display image
ima = imread(imgfile);
ff=figure(1); clf; imagesc(ima); axis image; axis ij; hold on;
% black and white images
if length(size(ima))<3
colormap(gray);
end
set(ff,'Position',SMALL_WINDOW);
%% show box
box_handle = rectangle('position', [box_coord(3), box_coord(1), box_coord(4)-box_coord(3), box_coord(2)-box_coord(1)]);
set(box_handle, 'edgecolor','y', 'linewidth',5);
%% show contour
for cc = 1:size(obj_contour,2)
if cc < size(obj_contour,2)
plot([obj_contour(1,cc), obj_contour(1,cc+1)]+box_coord(3), [obj_contour(2,cc), obj_contour(2,cc+1)]+box_coord(1), 'r','linewidth',4);
else
plot([obj_contour(1,cc), obj_contour(1,1)]+box_coord(3), [obj_contour(2,cc), obj_contour(2,1)]+box_coord(1), 'r','linewidth',4);
end
end
title(imgfile);
I do not know matlab but I was able to follow the provided script to plot a bounding box, but plotting the contour fails. This is my Python script:
img_dir = '/content/data/caltech101/101_ObjectCategories/chair/image_0037.jpg'
annot_dir = '/content/data/caltech101/Annotations/chair/annotation_0037.mat'
img = Image.open(img_dir_list[idx])
annot = sio.loadmat(annot_dir_list[idx])
obj_contour = annot['obj_contour']
print(obj_contour.shape[1])
for cc in range(obj_contour.shape[1]):
if cc < obj_contour.shape[1] - 1:
plt.plot([obj_contour[0,cc], obj_contour[0,cc+1]]+box_coord[3], [obj_contour[1,cc], obj_contour[1,cc+1]]+box_coord[1], 'r','linewidth',4)
else:
plt.plot([obj_contour[0,cc], obj_contour[0,0]]+box_coord[3], [obj_contour[1,cc], obj_contour[1,0]]+box_coord[1], 'r','linewidth',4)
I have added original image and contour map that the Python code produced here. As you can see the contour is flipped upside down.
If I subtract 200 from the y coordinate, the result looks ugly.
laxus clive is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1