How to detect rectangle region of one image and merge another image to that region

1.1k Views Asked by At

I'm new to image processing, i have to process image file jpeg1 as following:

1.Detect the text as "Text to detect!" on jpeg1

2.Detect the rectangle region as "Rectangle to be detected" and merge another image seal.jpeg to the rectangle area.

I don't know how to achieve that with C#?

My image file jpeg1 as: jpeg1 to process

The raw image jpeg1 as: jpeg1 to process The seal image to be merged as: seal to merge

1

There are 1 best solutions below

2
On

I can help with your question 2. I don't use C#, and I just wrote a simple Matlab code to extract the rectangular area you want. I assume opencv has those image processing toolbox as well.Please see my comments below.

file = 'https://i.stack.imgur.com/zPfdy.jpg';  % Your image provided above
img = imread(file);
img = rgb2gray(img);   % convert to gray scale image

% Canny edge detection with threshold of 0.5
img_edge = edge(img,'canny',0.5);

%Filled image between the edges
img_filled = imfill(img_edge,'holes');

%Find the filled region with the maximum area
props = regionprops(img_filled,'Area','PixelList');

max_area = props(1).Area;
max_count = 1;
for count=1:size(props,1)
if(max_area < props(count).Area)
    max_area = props(count).Area;
    max_count = count;
end
end
out_img = zeros([size(img_edge,1) size(img_edge,2)]);
pixels = props(max_count).PixelList;
for count=1:size(pixels,1)
    out_img(pixels(count,2),pixels(count,1)) = 1; % fill the maximum filled area
end
% % % % % % % % % % % % % % % % % % % % % % % % 
figure,imagesc(out_img.*double(img))

The result: enter image description here

Regarding the text detection, I think it is really tough without some prior information because you have so many text involved. It will be better that you know the approximate position of your text to detect, then we can try to look for the text within a much smaller region. Besides, it is also tricky to tune the threshold when you are targeting some specific text that probably has different intensity in gray image. By the way, I cannot process the image you provide because there is a strong rectangular region you added onto the text to be detected, which made me extract that region very easily, whereas it is not the case in reality.