I want to give a piece of code an object, and have it find what the closest thing to that object is
1 次查看(过去 30 天)
显示 更早的评论
So the current problem that I'm dealing with is that I have images where cells are compartmentalized in a channel, and I need to identify where this channel actually begins. This is not a machine learning image recognition problem, there is a channel in every photo -- I need the code to essentially, given what the channel looks like, try to find which part of the photo is most likely the channel, and return the position. Anyone got any clues on how I could potentially solve this? I realize this is a regression problem more or less, just wondering if there is a more elegant solution.
采纳的回答
Image Analyst
2017-8-16
Try this. Adjust the threshold to widen or narrow the detected region.
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'Screen Shot 2017-08-08 at 5.24.20 PM.png';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%===============================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
% greenChannel = rgbImage(:, :, 2);
% blueChannel = rgbImage(:, :, 3);
% Sum the red channel vertically to get the horizontal profile.
horizontalProfile = mean(redChannel, 1);
% Plot the profile.
subplot(2, 2, 2);
plot(horizontalProfile, 'r-', 'LineWidth', 2);
grid on;
title('Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');
% Find out where the profile exceeds some threshold.
threshold = 95;
aboveThreshold = horizontalProfile > threshold;
% Extract just the longest region.
aboveThreshold = bwareafilt(aboveThreshold, 1);
% Find the left-most column
startingColumn = find(aboveThreshold, 1, 'first')
% Find the right-most column
endingColumn = find(aboveThreshold, 1, 'last')
% Display the image again.
% Display the original image.
subplot(2, 2, 3:4);
imshow(rgbImage);
axis on;
caption = sprintf('Original Color Image, with starting and ending lines');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Draw line over it at the starting column
hold on;
line([startingColumn, startingColumn], [1, rows], 'Color', 'b', 'LineWidth', 3);
line([endingColumn, endingColumn], [1, rows], 'Color', 'b', 'LineWidth', 3);
% Draw lines over profile plot.
subplot(2, 2, 2);
hold on;
line([startingColumn, startingColumn], ylim, 'Color', 'b', 'LineWidth', 3);
line([endingColumn, endingColumn], ylim, 'Color', 'b', 'LineWidth', 3);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!