how to embed a watermark inside roi in matlab - matlab

the region of interest in the image are calculated. Now how to embed the watermark into the roi..i ve put the code for embedding it in the whole image in lsb. How can it modified for roi alone?
clear all;
file_name='pout.tif';
cover_object=imread(file_name);
file_name='cameraman.tif';
message=imread(file_name);
message=double(message);
message=round(message./256);
message=uint8(message);
Mc=size(cover_object,1);
Nc=size(cover_object,2);
Mm=size(message,1);
Nm=size(message,2);
for ii = 1:Mc
for jj = 1:Nc
watermark(ii,jj)=message(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
watermarked_image=cover_object;
for ii = 1:Mc
for jj = 1:Nc
watermarked_image(ii,jj)=bitset(watermarked_image(ii,jj),1,watermark(ii,jj));
end
end
imwrite(watermarked_image,'watermarkedimage','bmp');
figure(1)
imshow(watermarked_image,[])
title('Watermarked Image')

If your roi is rectangular, just loop over the appropriate subsection of the image rather than the whole thing.
If not, and you can define the watermarking as some function:
imgout = watermark(img1, img2);
Then you can use roifilt2 to apply that function just in your roi.
In this simple example,mask is a BW matrix where 1 indicates part of our roi (a mask can be created several different ways including using some of the interactive roi functions, see bottom section). img1 and img2 have already been loaded:
f = #(x) watermark(x,img2); % our very basic function
imgout = roifilt2(img1,mask,f);
Complications may arise if your img2 is smaller than img1 (or if you want to resize it to just cover the roi area). In this case, do everything on a subsection of img1/mask and then assemble the final image:
img1_s = img1(a:b,c:d); % these return the same size as img2
mask_s = mask(a:b,c:d);
imgout_s = roifilt2(img1_s,mask1_s,f);
imgout = img1;
imgout(a:b,c:d) = imgout_s;
A mask can be created several ways, some using the interactive roi functions, some not. A lot depends on how you have your roi in the first place - do you have a centre/radius, or do you want to hand-pick the location interactively, etc. Here are a few examples making use of the Image Processing Tool Box:
Interactive ROI function and createMask.
This will work for any of the interactive roi functions (imellipse, imfreehand, etc). You can interactively adjust the mask between the second and third steps. Do not close the image window before calling createMask.
h = imshow(img); %display image
e = imellipse(gca,[50 50 100 100]); % select roi
mask = createMask(e,h); % return mask
List of x/y points and poly2mask
If you have a list of points defining the outer edge of your roi, a quick non-interactive way of producing a mask is to use poly2mask. The third and fourth values define the total size of the mask returned.
mask = poly2mask(x,y,size(img,1),size(img,2));
Using morphological operators
strel defines a structuring element, and imdilate performs a dilation using that structuring element. Given an image which contains a single point, what that does is replace the point with the structuring element - in this case disk which will produce a circle (or as close as you can get using pixels). Other shapes (e.g. diamond) can be used with strel
mask = zeros(size(img));
mask(a,b) = 1; % a,b is the centre point of the circle
se = strel(disk,r,0); %r is the radius of the circle
mask = imdilate(mask,se);

Related

Changing Bounding Box Dimensions

New to Matlab here. I'm trying to implement some code to detect a face in an image and crop it. I have the script running, but the bounding box that it places around the detected face is a bit small. Is there any way to change the dimensions of the bounding box to capture more of the faces?
clc;
% cd into the a folder with pictures
cd 'C:\Users\abc\Desktop\folder'
files = dir('*.jpg');
for file = files'
img = imread(file.name);
figure(1),imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7;
BB = step(FaceDetect,img);
figure(2),imshow(img);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','- ','EdgeColor','r');
end
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','- ','EdgeColor','r');
J = imcrop(img,BB(i,:));
figure(3);
imshow(J);
a = 'edited\'
b = file.name
output = strcat(a,b);
imwrite(J,output);
end
%Code End
end
Currently, the script finds a face like so:
And outputs an image such as this:
This is good, I just want to extend the boundaries of the cropping zone to capture more of the face (e.g., hair and chin).
From the MATLAB rectangle function documentation.
rectangle('Position',pos) creates a rectangle in 2-D coordinates.
Specify pos as a four-element vector of the form [x y w h] in data
units. The x and y elements determine the location and the w and h
elements determine the size. The function plots into the current axes
without clearing existing content from the axes.
If you are just looking to increase the bounding box by some scale factor about the center of the rectangle, you could scale the w and h components in BB and adjust the rectangle origin x and y by subtracting half the scale difference. The following code should work if you place it right after the BB = step(FaceDetect,img); line in your code. I don't have MATLAB available to me at the moment but I'm pretty sure this will work.
% Scale the rectangle to 1.2 times its original size
scale = 1.2;
% Adjust the lower left corner of the rectangles
BB(:,1:2) = BB(:,1:2) - BB(:,3:4)*0.5*(scale - 1)
% Adjust the width and height of the rectangles
BB(:,3:4) = BB(:,3:4)*scale;
You can use imresize function in Matlab as described in this link and bboxresize to resize the bounding box
Below is the simple code to resize your image into 3 times the original one
%% clean workspace
clc;
clear;
cd 'C:\Users\abc\Desktop\folder';
files = dir('*.jpg');
for file = files'
img = imread(file.name) ;
figure(1),imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold =7;
BB = step(FaceDetect,img);
BB2 = BB;
%% Scale the rectangle to 3 times its original size
scale = 3;
%% Resize image
ImgResized = imresize(img,scale);
%% Resize bound box using the function named bboxresize in Matlab
BBResized = bboxresize(BB,scale);
figure(2),imshow(ImgResized);
%% Draw Bounding Box
for i=1:size(BBResized,1)
rectangle('position',BBResized(i,:),'lineWidth',2,'LineStyle','- ','EdgeColor','y');
end
end

Object extraction for a low contrast image

I have an image with very low contrast, from which I would like to extract a text object. Since it has a low contrast, I tried a few methods but no method gave me a satisfying result. I used watershed to extract the text object, but as the contrast is poor the extraction was not successful.
My program for watershed is:
I_cropped=imread(strcat('C:\Id\',currentfilename));
I_cropped = rgb2gray(I_cropped);
I_eq = histeq(I_cropped);
figure,imshow(I_eq);
bw = im2bw(I_eq, graythresh(I_eq));
bw2 = imfill(bw,'holes');
bw3 = imopen(bw2, ones(5,5));
bw4 = bwareaopen(bw3, 40);
bw = im2bw(I_eq, graythresh(I_eq));
figure,imshow(bw);
mask_em = imextendedmax(I_eq, 30);
mask_em = imclose(mask_em, ones(5,5));
mask_em = imfill(mask_em, 'holes');
mask_em = bwareaopen(mask_em, 40);
figure,imshow(mask_em);
I_eq_c = imcomplement(I_eq);
figure,imshow(I_eq_c);
I_mod = imimposemin(I_eq_c, ~bw4 | mask_em);
figure,imshow(I_mod);
L = watershed(I_mod);
figure,imshow(label2rgb(L));
I applied laplacian filter and to enhance edge, but it was not effective.
My objective is to extract text object. What method should I try for such low contrast image?
The image is attached:
Here is a way to do it.
First apply a median filter with a large kernel on the image to remove outliers and then apply a threshold to convert to a binary image. Note that playing with the kernel size of the filter alters the threshold level you need to use. Play around with it to see the output changing.
Then invert the image and apply regionprops to detect objects in the image. After that a little math to deduce the x and y origin (defining upper left corner) as well as the width and length of the large bounding box enclosing all the letters from the image.
Here is the code:
clear
clc
close all
Im = rgb2gray(imread('Imtext.png'));
%// Apply median filter to remove outliers
Im = medfilt2(Im,[9 9]);
%// Clear borders and apply threshold, then invert image
ImBW = imclearborder(~(im2bw(Im,.55)));
%// Find bounding boxes to delineate text regions.
S = regionprops(ImBW,'BoundingBox');
%// Concatenate all bounding boxes and obtain x,y, width and length of big
%// bounding box enclosing everything
bb = cat(1,S.BoundingBox);
LargeBB_x = min(bb(:,1));
LargeBB_y = min(bb(:,2));
LargeBB_height = max(bb(:,4));
%// Find last column in which pixel value is 1; that's the end
%// of the bounding box.
[~,ic] = find(ImBW==1);
MaxCol = max(ic(:));
LargeBB_width = MaxCol-LargeBB_x;
%// Display boxes
imshow(ImBW)
hold on
rectangle('Position',[LargeBB_x LargeBB_y LargeBB_width LargeBB_height],'EdgeColor','r','LineWidth',2)
hold off
And the output:
Or with the original image:

Matlab: Crop objects from binary image

I'm new to matlab, I have a image and i want to crop all the three circles and store them. My code works for single circle in an image. But fails to work when i have more circles in image.
My code:
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
rectangle('Position', s.BoundingBox);
imCrop = imcrop(bw2, s.BoundingBox);
figure, imshow(imCrop);
Any ideas about it?
You almost have it working. Bear in mind that when you do s.BoundingBox by itself, you are only extracting the first circle. As such, I would recommend you make a cell array that stores the individual circles for each bounding box, then run a for loop through all of your bounding boxes. As such, each element in your cell array would be a cropped circle. As such, try doing this:
%// Your code
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
%// New code here
circles = cell(1,numel(s));
for idx = 1 : numel(s)
rect = s(idx).BoundingBox;
circles{idx} = imcrop(bw2, rect);
end
circles will now be a cell array of cropped circles. To access the ith circle, simply do:
imCrop = circles{i};
Edit
From your comments, you want to detect the largest and smallest circles. This can easily be done by checking the Area attribute from regionprops. You would find the bounding box that generates the minimum and maximum areas. You would need to modify your regionprops call to include the Area flag. As such:
s = regionprops(bw2, 'BoundingBox', 'Area');
[~,indMin] = min([s.Area]);
[~,indMax] = max([s.Area]);
circleSmall = circles{indMin};
circleLarge = circles{indMax};
The above code will find the circles with the minimum and maximum area, then extract those corresponding circles, assuming you've run the code to extract all of those circles in that for loop I wrote earlier. Bear in mind that I had to enclose s.Area in square braces. The reason why is because when you do this, you'll be able to extract all of the areas as a single array instead of a matrix with singleton dimensions, and min/max can't work on something like that.

Matlab: Matrix manipulation: Change values of pixels around center pixel in binary matrix

I have another problem today:
I have a binary matrix t, in which 1 represents a river channel, 0 represents flood plane and surrounding mountains:
t = Alog>10;
figure
imshow(t)
axis xy
For further calculations, I would like to expand the area of the riverchannel a few pixels in each direction. Generally speaking, I want to have a wider channel displayed in the image, to include a larger region in a later hydraulic model.
Here is my attempt, which does work in certain regions, but in areas where the river runs diagonal to the x-y axis, it does not widen the channel. There seems to be a flow in approaching this, which I cannot quite grasp.
[q,o] = find(t == 1);
qq = zeros(length(q),11);
oo = zeros(length(o),11);
% add +-5 pixel to result
for z=1:length(q)
qq(z,:) = q(z)-5:1:q(z)+5;
oo(z,:) = o(z)-5:1:o(z)+5;
end
% create column vectors
qq = qq(:);
oo = oo(:);
cords = [oo qq]; % [x y]
% remove duplicates
cords = unique(cords,'rows');
% get limits of image
[limy limx] = size(t);
% restrict to x-limits
cords = cords(cords(:,1)>=1,:);
cords = cords(cords(:,1)<=limx,:);
% restrict to y-limits
cords = cords(cords(:,2)>=1,:);
cords = cords(cords(:,2)<=limy,:);
% test image
l = zeros(size(img));
l(sub2ind(size(l), cords(:,2)',cords(:,1)')) = 1;
figure
imshow(l)
axis xy
This is the image I get:
It does widen the channel in some areas, but generally there seems to be a flaw with my approach. When I use the same approach on a diagonal line of pixels, it will not widen the line at all, because it will just create more pairs of [1 1; 2 2; 3 3; etc].
Is there a better approach to this or even something from the realm of image processing?
A blur filter with a set diameter should be working somewhat similar, but I could not find anything helpful...
PS: I wasn't allowed to add the images, although I already have 10 rep, so here are the direct links:
http://imageshack.us/a/img14/3122/channelthin.jpg
http://imageshack.us/a/img819/1787/channelthick.jpg
If you have the image processing toolbox, you should use the imdilate function. This performs the morphological dilation operation. Try the following code:
SE = strel('square',3);
channelThick = imdilate(channelThin,SE);
where SE is a 3x3 square structuring element used to dilate the image stored in channelThin. This will expand the regions in channelThin by one pixel in every direction. To expand more, use a larger structuring element, or multiple iterations.
You may apply morphological operations from image processing. Morphological dilation can be used in your example.
From the image processing toolbox, you can use bwmorth command BW2 = bwmorph(BW,'dilate') or imdilate command IM2 = imdilate(IM,SE).
Where IM is your image and SE is the structuring element. You can set SE = ones(3); to dilate the binary image by "one pixel" - but it can be changed depending on your application. Or you can dilate the image several times with the same structuring element if needed.

Get outer indecies of a region in a logical image

If I've any m x n logical image of a white region like the following:
How to get the indices of the boundary line between the white and black regions?
This simply comes down to detecting the edges of the given image. MATLAB already has a built-in implementation for that in the edge command. Here's an example of detecting the boundaries of an image I using the Canny filter:
A = edge(I, 'canny');
The non-zero elements in the resulting image A are what you're after. You can then use find to obtain their indices.
Since your input is a clear binary image, there is no need to use edge as suggested by #EitanT.
Getting the perimeter using morphological operations imdilate, imerode and regionprops:
% let input image be bw
we = bw & ~imerode( bw, strel('disk', 1) ); % get a binary image with only the boundary pixels set
st = regionprops(we, 'PixelIdxList'); % get the linear indices of the boundary
% get a binary image with pixels on the outer side of the shape set
be = ~bw & imdilate( bw, strel('disk', 1) );
st = regionprops(be, 'PixelList'); % get the row-col indices of the boundary