-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Circle packing, dominant color descriptor and button database impleme…
…nted
- Loading branch information
0 parents
commit 00c6eb8
Showing
15 changed files
with
644 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
/*/ | ||
!.gitignore | ||
!README.md | ||
!LICENSE | ||
!/buttons/ | ||
!/source/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Linus Mossberg | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
This is a database with images of buttons sourced from various places. All source images were licensed to permit sharing and adaption. The source images have been modified by cropping, masking and resizing them. In some cases, source images containing multiple buttons have also been split up into smaller images of individual buttons. | ||
|
||
The filenames uses the following formatting: | ||
[source-creator-label]_[licence]_[image-number].png | ||
|
||
The file buttons.mat contains information about the 3 dominant colors and the mean colors of each button. | ||
|
||
--------------------------------------------------- | ||
|
||
Attributions: | ||
|
||
Source creator label: andy-johnson | ||
Website: https://www.flickr.com/people/93410621@N05 | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: arlene-janner | ||
Website: https://www.flickr.com/people/106074308@N06/ | ||
License: https://creativecommons.org/licenses/by/2.0/ | ||
|
||
Source creator label: volvob12b | ||
Website: https://www.flickr.com/people/volvob12b/ | ||
License: https://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
Source creator label: twenty-questions | ||
Website: https://www.flickr.com/people/twenty_questions | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
||
Source creator label: markmorgantrinidad | ||
Website: https://www.flickr.com/people/markmorgantrinidad | ||
License: https://creativecommons.org/licenses/by/2.0/ | ||
|
||
Source creator label: presley-m | ||
Website: https://www.flickr.com/people/presley_m/ | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: silvia-siri | ||
Website: https://www.flickr.com/people/130331218@N03/ | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: mag3737 | ||
Website: https://www.flickr.com/people/mag3737/ | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: littlelixie | ||
Website: https://www.flickr.com/people/littlelixie/ | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
||
Source creator label: obd-design | ||
Website: https://www.flickr.com/people/obd-design | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: pikrepo | ||
Website: https://pikrepo.com/ | ||
License: https://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
Source creator label: pixbay | ||
Website: https://pixbay.com/ | ||
License: https://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
Source creator label: pixnio | ||
Website: https://pixnio.com/ | ||
License: https://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
Source creator label: readyelements | ||
Website: http://www.readyelements.com/ | ||
License: https://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
Source creator label: salvagenation | ||
Website: https://www.flickr.com/people/salvagenation | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: scrappy-annie | ||
Website: https://www.flickr.com/people/14903992@N08 | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
||
Source creator label: shellysblogger | ||
Website: https://www.flickr.com/people/shellysblogger/ | ||
License: https://creativecommons.org/licenses/by-nc-sa/2.0/ | ||
|
||
Source creator label: thevintagesailor | ||
Website: https://www.flickr.com/people/thevintagesailor/ | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
||
Source creator label: vaula | ||
Website: https://www.flickr.com/people/23882161@N03/ | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
||
Source creator label: welshkaren | ||
Website: https://www.flickr.com/people/welshkaren | ||
License: https://creativecommons.org/licenses/by-nc/2.0/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function [L, circles] = addCircle(image, circles, centroid, radius, L) | ||
|
||
new_circle = struct; | ||
|
||
[height, width] = size(image); | ||
|
||
f_radius = ceil(radius); | ||
s_idx = floor(centroid - [f_radius, f_radius]); | ||
e_idx = ceil(centroid + [f_radius, f_radius]); | ||
|
||
s_idx(2) = clamp(s_idx(2), 1, height); | ||
e_idx(2) = clamp(e_idx(2), 1, height); | ||
s_idx(1) = clamp(s_idx(1), 1, width); | ||
e_idx(1) = clamp(e_idx(1), 1, width); | ||
|
||
%%% | ||
|
||
x_range = s_idx(2):e_idx(2); | ||
y_range = s_idx(1):e_idx(1); | ||
|
||
[Y, X] = meshgrid(y_range, x_range); | ||
circle = (X - centroid(2)).^2 + (Y - centroid(1)).^2 <= radius^2; | ||
|
||
region = L(x_range, y_range); | ||
region(circle) = false; | ||
L(x_range, y_range) = region; | ||
|
||
rgb_region = image(x_range, y_range, :); | ||
colors = reshape(rgb_region, [], 3); | ||
colors = colors(circle(:), :); | ||
|
||
new_circle.dominant_colors = kDominantColors(colors, 3, 10000, 3); | ||
new_circle.radius = radius; | ||
new_circle.position = centroid; | ||
|
||
circles = [circles new_circle]; | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function result = clamp(value, lower, upper) | ||
result = min(max(value, lower), upper); | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
function createButtonDatabase(folder, out_folder) | ||
|
||
return; % avoid accidentally running and ruining db | ||
|
||
clear saveImage; | ||
|
||
readme_id = fopen(strcat(out_folder, '\README.txt'), 'a'); | ||
addReadmeHeader(readme_id); | ||
|
||
attribution_entry = 'Source creator label: %s\nWebsite: %s\nLicense: %s\n\n'; | ||
|
||
feather_pixels = 5; | ||
|
||
directories = dir(folder); | ||
for directory = directories' | ||
|
||
path = strcat(directory.folder, '\', directory.name); | ||
info_filename = strcat(path, '\info.json'); | ||
|
||
if(directory.isdir && isfile(info_filename)) | ||
|
||
info_json = jsondecode(fileread(info_filename)); | ||
|
||
fprintf(readme_id, attribution_entry, info_json.name, ... | ||
info_json.website, info_json.license); | ||
|
||
jpg_files = dir([path '\*.jpg']); | ||
png_files = dir([path '\*.png']); | ||
|
||
prefix = strcat(info_json.name, '_', info_json.license_name); | ||
|
||
for jpeg_file = jpg_files' | ||
image = imread(strcat(jpeg_file.folder, '\', jpeg_file.name)); | ||
[image, alpha] = cropAndMaskCircle(image, feather_pixels); | ||
saveImage(image, alpha, prefix, out_folder); | ||
end | ||
|
||
for png_file = png_files' | ||
image_path = strcat(png_file.folder, '\', png_file.name); | ||
|
||
% Twice because reading rgb with alpha is buggy | ||
[~,~,alpha] = imread(image_path); | ||
image = imread(image_path); | ||
|
||
mask = alpha >= 128; | ||
|
||
CC = bwconncomp(mask, 4); | ||
BB = regionprops('table', CC, 'BoundingBox').BoundingBox; | ||
|
||
for i = 1:size(BB, 1) | ||
cropped_image = imcrop(image, BB(i, :)); | ||
[cropped_image, alpha] = cropAndMaskCircle(cropped_image, feather_pixels); | ||
saveImage(cropped_image, alpha, prefix, out_folder); | ||
end | ||
end | ||
end | ||
end | ||
fclose(readme_id); | ||
end | ||
|
||
function [image, alpha] = cropAndMaskCircle(image, feather_pixels) | ||
|
||
max_resolution = 1024; | ||
|
||
[height, width, ~] = size(image); | ||
dim = min(height, width); | ||
r = centerCropWindow2d(size(image), [dim, dim]); | ||
image = imcrop(image, r); | ||
|
||
if(dim > max_resolution) | ||
dim = max_resolution; | ||
image = imresize(image, [dim, dim]); | ||
end | ||
|
||
[X, Y] = meshgrid(1:dim, 1:dim); | ||
center = (dim + 1) / 2; | ||
radius = dim / 2; | ||
|
||
center_dist = sqrt((X - center).^2 + (Y - center).^2); | ||
feather_radius = radius - feather_pixels; | ||
|
||
alpha = (center_dist - feather_radius) / (radius - feather_radius); | ||
alpha = 1 - clamp(alpha, 0, 1); | ||
alpha = easeInEaseOut(alpha, 1.5); | ||
|
||
alpha = im2uint8(alpha); | ||
image = im2uint8(image); | ||
end | ||
|
||
function saveImage(image, alpha, prefix, folder) | ||
persistent prefixes; | ||
|
||
if isempty(prefixes) | ||
prefixes = containers.Map; | ||
end | ||
|
||
if(~prefixes.isKey(prefix)) | ||
prefixes(prefix) = 0; | ||
end | ||
|
||
prefixes(prefix) = prefixes(prefix) + 1; | ||
|
||
filename = strcat(prefix, '_', num2str(prefixes(prefix), '%04d'), '.png'); | ||
|
||
imwrite(image, strcat(folder, '\', filename), 'Alpha', alpha); | ||
|
||
db_info_filename = strcat(folder, '\buttons.mat'); | ||
|
||
colors = reshape(image, [], 3); | ||
mask = alpha > 128; | ||
colors = im2double(colors(mask(:), :)); | ||
|
||
entry.dominant_colors = kDominantColors(colors, 3, 20000, 5); | ||
entry.mean_color_lab = mean(rgb2lab(colors)); | ||
entry.filename = filename; | ||
entry.diameter = size(image, 1); | ||
|
||
if ~isfile(db_info_filename) | ||
data = entry; | ||
save(db_info_filename, 'data', '-v7.3'); | ||
else | ||
m = matfile(db_info_filename, 'Writable', true); | ||
m.data(end+1,:) = entry; | ||
end | ||
end | ||
|
||
function addReadmeHeader(readme_id) | ||
header = ['This is a database with images of buttons sourced from various places. ', ... | ||
'All source images were licensed to permit sharing and adaption. ', ... | ||
'The source images have been modified by cropping, masking and resizing them. ', ... | ||
'In some cases, source images containing multiple buttons have also been split up into smaller images of individual buttons.\n\n', ... | ||
'The filenames uses the following formatting:\n', ... | ||
'[source-creator-label]_[licence]_[image-number].png\n\n', ... | ||
'The file buttons.mat contains information about the 3 dominant colors and the mean colors of each button.\n\n', ... | ||
'---------------------------------------------------\n\n', ... | ||
'Attributions:\n\n']; | ||
|
||
fprintf(readme_id, header); | ||
end | ||
|
||
function y = easeInEaseOut(x, a) | ||
y = x.^a ./ (x.^a + (1-x).^a); | ||
end | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
function similarity = dominantColorsSimilarity(DC1, DC2, num_nodes) | ||
|
||
% Returns a value in range [negative largest lab color distance, 0], where | ||
% large values means that the dominant color descriptors DC1 and DC2 are | ||
% similar. The dominant color descriptors consists of the K most dominant | ||
% colors of a set of colors, with a dominance percentage assigned to each | ||
% dominant color. They are generated by kDominantColors.m using k-means. | ||
% Larger num_nodes increases the accuracy, but the time complexity is | ||
% something like O(num_nodes^3), depending on the algorithm used by matchpairs. | ||
|
||
% This similarity measure is based on the OCCD measure presented in the paper: | ||
% | ||
% Extraction of Perceptually Important Colors and Similarity Measurement | ||
% for Image Matching, Retrieval, and Analysis, 2002 | ||
% Aleksandra Mojsilovic, Jianying Hu and Emina Soljanin | ||
% | ||
% I.e create a set of num_nodes nodes for each of the two dominant color | ||
% descriptors and then distribute the dominant colors across these nodes | ||
% based on the dominance percentage of the colors. Then connect | ||
% the nodes between these two sets to create a bipartite graph, and define | ||
% the weight/cost of each edge to be the distance between the colors of the | ||
% connected nodes. | ||
% | ||
% Then it's a matter of selecting the best pairs between the sets by | ||
% selecting the connections that minimizes the total cost of the resulting | ||
% graph. This is called the Assignment Problem and it can be solved using | ||
% the Hungarian/Munkres algorithm for example. Here I use the built in | ||
% 'matchpairs' function. | ||
|
||
% Quantize dominance distribution into discrete nodes with indexes to | ||
% the corresponding dominant color | ||
nodes1 = quantizeDistribution(DC1.dominance, num_nodes); | ||
nodes2 = quantizeDistribution(DC2.dominance, num_nodes); | ||
|
||
% Calculate pair-wise color distances. dists(i,j) is the distance between | ||
% dominant color i in DC1 and dominant color j in DC2. | ||
dists = pdist2(DC1.colors_lab, DC2.colors_lab); | ||
|
||
% Create cost table / bipartite graph edge weights. | ||
costs = dists(nodes1, nodes2); | ||
|
||
% Find the pairings that minimizes the total cost | ||
matched = matchpairs(costs, 1e6); | ||
|
||
% Find the total cost by summing the cost of each pairing. | ||
% Use (col-1)*row_width+row to vectorize lookup | ||
unsimilarity = sum(costs((matched(:,2) - 1) * num_nodes + matched(:,1))); | ||
|
||
% Normalize by dividing by the number of nodes used to quantize the | ||
% dominant colors so that the result is similar regardless of nodes. | ||
similarity = -unsimilarity / num_nodes; | ||
end | ||
|
||
function result = quantizeDistribution(distribution, num_elements) | ||
counts = sumPreservingRound(distribution * num_elements); | ||
ranges = cumsum(counts); | ||
result = ones(1, num_elements, 'uint32'); | ||
for i = 2:length(ranges) | ||
result(ranges(i - 1) + 1:ranges(i)) = i; | ||
end | ||
end |
Oops, something went wrong.