Skip to content

Commit

Permalink
add bbs generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fujenchu committed May 30, 2018
1 parent 72255a2 commit 4201cc1
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
Binary file added imgs/color3.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/hiwCz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions utils/boundingBoxGenerator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
clear
close all
DEBUG=false
ROOT = 'tools/';
SAVE_ROOT = 'tools_bbs/';
listFolders = dir(ROOT);
fileID = fopen('toolsBBS4.txt','w');

for folderIdx = 1:length(listFolders)
folderName = listFolders(folderIdx).name;
listImgs = dir([ROOT folderName '/*.jpg']);
fprintf(['processing ' folderName ' ..\n']);
mkdir([SAVE_ROOT folderName '_bbs']);

for imgIdx = 1:length(listImgs)
imgName = listImgs(imgIdx).name;
img = imread([ROOT folderName '/' imgName]);
[filepath, name, ext] = fileparts(imgName);
imshow(img);

BLUE_RANGE = [220,250]/360;
INTENSITY_T = 0.1;
hsv = rgb2hsv(img);
blueAreasMask = hsv(:,:,1)>BLUE_RANGE(1) & hsv(:,:,1) < BLUE_RANGE(2) & hsv(:,:,3) > INTENSITY_T & hsv(:,:,2) > 0.33;
if DEBUG
figure; imagesc(blueAreasMask);
end

BW = blueAreasMask;
CC = bwconncomp(blueAreasMask);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
for i=1:length(CC.PixelIdxList)
if i ~= idx
BW(CC.PixelIdxList{i}) = 0;
end
end
if DEBUG
figure;imshow(BW);
end


J = imcomplement(BW);
if DEBUG
figure; imshow(J);
end

CC = bwconncomp(J);
numPixels = cellfun(@numel,CC.PixelIdxList);
[piexlnum,index] = sort(numPixels);
idy = index(end-1);
for i=1:length(CC.PixelIdxList)
if i ~= idy
J(CC.PixelIdxList{i}) = 0;
end
end
if DEBUG
figure;imshow(J);
end


structBoundaries = bwboundaries(J);
xy=structBoundaries{1};
rightBottom = max(xy);
topLeft = min(xy);

figure(1)
hold on
plot([rightBottom(2) rightBottom(2) topLeft(2) topLeft(2) rightBottom(2)], [rightBottom(1) topLeft(1) topLeft(1) rightBottom(1) rightBottom(1)], 'color', 'r')
%pause(0.01)

fprintf(fileID, '%s %f %f %f %f\n', name, topLeft(2), topLeft(1), rightBottom(2), rightBottom(1) );
F=getframe;
imwrite(F.cdata, [SAVE_ROOT folderName '_bbs/seg_' imgName]);
end
end
fclose(fileID)
69 changes: 69 additions & 0 deletions utils/boundingBoxGenerator_knife06_whiteBG.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

% HSV
% Hue color range
% Saturation > 0.33 filter out while
% Value > 0.1 to filter out black
% https://www.google.com/search?q=hsv+color&tbm=isch&source=iu&ictx=1&fir=jm5gKKoOrirrYM%253A%252C7nZ7XrlSo8mOsM%252C_&usg=__1LEreHELgGosmR_p0szoExnbwCA%3D&sa=X&ved=0ahUKEwj4x8Oo46vbAhWDu1MKHdlvAlwQ9QEIgAEwCA#imgrc=HCuCoOIDsYvMAM:
%

clear
close all
DEBUG=false
ROOT = 'tools/';
SAVE_ROOT = 'tools_bbs/';
listFolders = dir(ROOT);
fileID = fopen('toolsBBS7.txt','w');

for folderIdx = 28:length(listFolders)
folderName = listFolders(folderIdx).name;
listImgs = dir([ROOT folderName '/*.jpg']);
fprintf(['processing ' folderName ' ..\n']);
mkdir([SAVE_ROOT folderName '_bbs']);

for imgIdx = 1:length(listImgs)
imgName = listImgs(imgIdx).name;
img = imread([ROOT folderName '/' imgName]);
[filepath, name, ext] = fileparts(imgName);
imshow(img);

BLUE_RANGE = [220,250]/360;
INTENSITY_T = 0.1;
hsv = rgb2hsv(img);
blueAreasMask = hsv(:,:,1)>BLUE_RANGE(1) & hsv(:,:,1) < BLUE_RANGE(2) & hsv(:,:,3) > INTENSITY_T& hsv(:,:,2) >0.33;
if DEBUG
figure; imagesc(blueAreasMask);
end

BW = blueAreasMask;

J=BW;
CC = bwconncomp(J);
numPixels = cellfun(@numel,CC.PixelIdxList);
[piexlnum,index] = sort(numPixels);
idy = index(end-1);
for i=1:length(CC.PixelIdxList)
if i ~= idy
J(CC.PixelIdxList{i}) = 0;
end
end
if DEBUG
figure;imshow(J);
end


structBoundaries = bwboundaries(J);
xy=structBoundaries{1};
rightBottom = max(xy);
topLeft = min(xy);

figure(1)
hold on
plot([rightBottom(2) rightBottom(2) topLeft(2) topLeft(2) rightBottom(2)], [rightBottom(1) topLeft(1) topLeft(1) rightBottom(1) rightBottom(1)], 'color', 'r')
%pause(0.01)

fprintf(fileID, '%s %f %f %f %f\n', name, topLeft(2), topLeft(1), rightBottom(2), rightBottom(1) );
F=getframe;
imwrite(F.cdata, [SAVE_ROOT folderName '_bbs/seg_' imgName]);
end
end
fclose(fileID)
100 changes: 100 additions & 0 deletions utils/move_model_files_affordance.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
%move files from one directory to another directory

dataRoot = '/home/fujenchu/projects/robotArm/catkin_ws/src/simData/warehouse/models';
dataDir= dir(dataRoot);

for idx_folder = 3:length(dataDir)
FolderName = dataDir(idx_folder).name;
fileRoot = [dataRoot '/' FolderName '/' FolderName '_origin.dae'];
[pathstr,filename] = fileparts(fileRoot);
mkdir([dataRoot '/' FolderName '/mesh']);
copyfile([dataRoot '/' FolderName '/*.dae'], [dataRoot '/' FolderName '/mesh']);
mkdir([dataRoot '/' FolderName '/sdf']);
fsdf = fopen([dataRoot '/' FolderName '/sdf/description.sdf'],'wt');
fprintf(fsdf, '<?xml version=''1.0''?>\n'); %write ' to a file needs '' in the string
fprintf(fsdf, '<sdf version="1.4">\n');
fprintf(fsdf, ['<model name="' filename '">\n']);
fprintf(fsdf, ' <static>true</static>\n');
fprintf(fsdf, ' <link name="link">\n');
fprintf(fsdf, ' <collision name="collision">\n');
fprintf(fsdf, ' <surface>\n');
fprintf(fsdf, ' <contact>\n');
fprintf(fsdf, ' <collide_without_contact>true</collide_without_contact>\n');
fprintf(fsdf, ' </contact>\n');
fprintf(fsdf, ' </surface>\n');
fprintf(fsdf, ' <geometry>\n');
fprintf(fsdf, ' <box>\n');
fprintf(fsdf, ' <size>0.1 0.1 0.1</size>\n');
fprintf(fsdf, ' </box>\n');
fprintf(fsdf, ' </geometry>\n');
fprintf(fsdf, ' </collision>\n');
fprintf(fsdf, ' <visual name="visual">\n');
fprintf(fsdf, ' <geometry>\n');
fprintf(fsdf, ' <mesh>\n');
fprintf(fsdf, [' <uri>model://' FolderName '/mesh/' filename '.dae</uri>\n']);
fprintf(fsdf, ' <scale>1 1 1</scale>\n');
fprintf(fsdf, ' </mesh>\n');
fprintf(fsdf, ' </geometry>\n');
fprintf(fsdf, ' </visual>\n');
fprintf(fsdf, ' </link>\n');
fprintf(fsdf, ' </model>\n');
fprintf(fsdf, '</sdf>');
fclose(fsdf);
allFiles = dir(pathstr);
dirFlags = [allFiles.isdir];
subFolders = allFiles(dirFlags);
%% move color folders into /mesh foler
idx = 1;
while idx <= length(subFolders)
if strcmp(subFolders(idx).name,'mesh') || strcmp(subFolders(idx).name,'sdf')
subFolders(idx)=[];
else
idx = idx +1;
end
end
for idx = 3:length(subFolders)
movefile(fullfile(dataRoot, FolderName, subFolders(idx).name), fullfile(dataRoot, FolderName, 'mesh'));
end

end

for idx_folder = 3:length(dataDir)
for idx_aff = 1:7
FolderName = dataDir(idx_folder).name;
fileRoot = [dataRoot '/' FolderName '/' FolderName '_origin.dae'];
[pathstr,filename] = fileparts(fileRoot);
mkdir([dataRoot '/' FolderName '/mesh']);
copyfile([dataRoot '/' FolderName '/*.dae'], [dataRoot '/' FolderName '/mesh']);
mkdir([dataRoot '/' FolderName '/sdf']);
fsdf = fopen([dataRoot '/' FolderName '/sdf/description_' num2str(idx_aff) '.sdf'],'wt');
fprintf(fsdf, '<?xml version=''1.0''?>\n'); %write ' to a file needs '' in the string
fprintf(fsdf, '<sdf version="1.4">\n');
fprintf(fsdf, ['<model name="' filename '">\n']);
fprintf(fsdf, ' <static>true</static>\n');
fprintf(fsdf, ' <link name="link">\n');
fprintf(fsdf, ' <collision name="collision">\n');
fprintf(fsdf, ' <surface>\n');
fprintf(fsdf, ' <contact>\n');
fprintf(fsdf, ' <collide_without_contact>true</collide_without_contact>\n');
fprintf(fsdf, ' </contact>\n');
fprintf(fsdf, ' </surface>\n');
fprintf(fsdf, ' <geometry>\n');
fprintf(fsdf, ' <box>\n');
fprintf(fsdf, ' <size>0.1 0.1 0.1</size>\n');
fprintf(fsdf, ' </box>\n');
fprintf(fsdf, ' </geometry>\n');
fprintf(fsdf, ' </collision>\n');
fprintf(fsdf, ' <visual name="visual">\n');
fprintf(fsdf, ' <geometry>\n');
fprintf(fsdf, ' <mesh>\n');
fprintf(fsdf, [' <uri>model://' FolderName '/mesh/' filename '_gt_' num2str(idx_aff) '.dae</uri>\n']);
fprintf(fsdf, ' <scale>1 1 1</scale>\n');
fprintf(fsdf, ' </mesh>\n');
fprintf(fsdf, ' </geometry>\n');
fprintf(fsdf, ' </visual>\n');
fprintf(fsdf, ' </link>\n');
fprintf(fsdf, ' </model>\n');
fprintf(fsdf, '</sdf>');
fclose(fsdf);
end
end

0 comments on commit 4201cc1

Please sign in to comment.