i want to extract fluorescent marked text from an image and convert the extracted image to another image file. how do i do that? i am new to matlab.

 Accepted Answer

Anand: Here, I did it for you.
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 = 20;
% Read in a color image.
folder = 'C:\Users\anand\Documents\Temporary';
baseFileName = 'anand.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
binaryImage = redChannel == 255 & greenChannel == 255 & blueChannel == 0;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
% There seem to be thin lines running through the image
% that break it up into ways we don't want.
% Dilate the image to fill the gaps.
binaryImage = imdilate(binaryImage, true(3));
% Fill the image
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage);
axis on;
title('Filled Binary Image', 'FontSize', fontSize);
% Label the image
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
% Measure the image
measurements = regionprops(labeledImage, 'BoundingBox');
% Display them
subplot(2, 2, 4);
for k = 1 : numberOfBlobs
thisBB = measurements(k).BoundingBox
thisCroppedImage = imcrop(rgbImage, thisBB);
imshow(thisCroppedImage);
axis on;
caption = sprintf('Region #%d out of %d', k, numberOfBlobs);
title(caption, 'FontSize', fontSize);
% Ask user if they want to continue;
promptMessage = sprintf('This is region #%d out of %d.\nDo you want to Continue processing,\nor Cancel to abort processing?',...
k, numberOfBlobs);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
end

10 Comments

thank you very much Image Analyst... i will ask you, if i have further doubts... thank you very much....
hello.. i m successful in applying this operation on images.. but i want to perform this operation on pdf files.. how do i do that?? since matlab doesnot allow operation on pdf files... can someone please provide me the code.. thank you very much...
There is an OCR capability built into Adobe Acrobat. You simply use that while you have your document open in Acrobat.
thank u so much.. but can u please tell me how to do it from matlab, without opening the adobe acrobat?? i mean is it possible to write code, so that when we import pdf into matlab it converts into images???
and there is one more problem... its showing index out of dimensions error when i upload such images and run the algorithm to extract fluoroscently marked text.. what do i do?? please help..its urgent....
Upload an image of a different size so I can check it. MATLAB does not have any OCR capabilities, as Acrobat does - you'll probably have to write your own if you insist on using MATLAB.
That image has basically been ruined by jpegging it so that you don't have perfect yellow (255,255,0) anymore. Zoom in on it and look at the colors. To handle this bad image, you'll have to replace the thresholding line with something like this:
binaryImage = redChannel >= 215 & greenChannel >= 215 & blueChannel <= 20;
I'd advise you to NEVER jpeg images if you need to do image analysis on them.
thank you so much.. the older algorithm is working perfectly on png images...and the new one works on jpeg images..thank u very much....

Sign in to comment.

More Answers (2)

1. Use a color segmentation algorithm to generate a mask where white pixels correspond to the yellow highlights in your input image. To do that you can:
2. Once you have a binary mask, fill the holes left by back letters using imfill. That will give you solid blocks of white pixels on a black background.
3. "Crop" the highlighted text from your input image using that mask. The mask will be a black and white image with 1 channel, but your input image is RGB (3 channels). You can execute the following to perform that operation:
% First, convert B&W mask to RGB
% mask_bw has size (n x m x 1)
% mask_rgb will have size (n x m x 3)
mask_rgb = repmat(mask_bw,1,1,3);
% Then, the mask indicates the location of pixels in your ROI
% Use it to change the color of other pixels to white
% in_img is your input image and has size (n x m x 3)
out_img = in_img;
out_img(~mask_rgb) = 1;

4 Comments

thank u so much... i m new to matlab.. can you please provide me the code so that i can analyse it and work on it... thank you so much
Take a look at the code here:
Open SimpleColorDetectionByHue.m, go to lines 226-231 and change the values to the following:
hueThresholdLow = 0.10;
hueThresholdHigh = 0.20;
saturationThresholdLow = 0.8;
saturationThresholdHigh = 1.0;
valueThresholdLow = 0.8;
valueThresholdHigh = 1.0;
They should work for your image.
Then, run the function by executing SimpleColorDetectionByHue in the Command Window and, when asked, choose to provide your own image (anand.png).
See the steps of the algorithm in action. Then, close the window and go back to the source code to see how it is done.
The steps are basically:
  • Convert RGB image to HSV
  • Apply lower and upper thresholds in HSV space
  • Get combined mask
  • Remove components that are too small from the mask with bwareaopen
  • Smooth mask border with morphological closing using imclose
  • Fill holes in the mask with imfill
  • Apply mask to original RGB image
You can search for functions you don't know in the documentation to help you understand. Play around with the different parameters -- like the size of the structural element used in morphological closing, for example -- to get the result you want.
please let me know what will be the values of hueThresholdLow, hueThresholdHigh , saturationThresholdLow , saturationThresholdHigh , valueThresholdLow, valueThresholdHigh , these parameters for brown color, black color magenda color
Depends on your image. See the Color Thresholder app on the Apps tab of the tool ribbon.

Sign in to comment.

Thanks a lot.. but i have one problem.. after it the highlighted text is only shown, i want to crop all the individually highlighted text and paste it into another file.. for example, in this portion there are two paragraphs of highlighted text.. i want to crop those parts only and paste these two paragraphs in another file.. can u help me,please? thanks a lot...

2 Comments

Not sure what you mean? Do you want the bounding box of the yellow regions cropped out into separate images? Just like the original (black text on yellow background), but cropped? If the yellow "Pure" yellow, in other words, r=255, G=255, and B = 0?
yes that is what i exactly want.. can you please provide me the code... thank u very much...

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!