Segmented blur fails when adding segments on image

2 views (last 30 days)
I have an image and I have an focus (rectangular) area which will be clear in result. I need to find another rectangular area larger than focus area. First keep focus area and other found area in a variable to user later. Now blur whole image much and then blur another area we found less than whole image and put it back. At last, put the focus back on image. What is wrong with my code?
Input Image
img.jpg
result image
result.png
x1,y1,x2,y2 are retes of coordinates. if x1 = 0.1 on 1020 pixel image x = 102
function blurred_image = blurImage(image, x1, y1, x2, y2)
[height, width, ~] = size(image);
%desired area coordinates
c_x1 = floor(width * x1);
c_y1 = floor(height * (1-y1));
c_x2 = floor(width * x2);
c_y2 = floor(height * (1-y2));
%blurred segment
b_x1 = floor(abs(c_x1 - c_x1/2));
b_y1 = floor(abs(c_y1 - c_y1/2));
b_x2 = floor(abs(c_x2 + (width - c_x2)/2));
b_y2 = floor(abs(c_y2 + (height - c_y2)/2));
fprintf("%d\n", b_x1);
fprintf("%d\n", b_y1);
fprintf("%d\n", b_x2);
fprintf("%d\n", b_y2);
focus = image(c_y1:c_y2, c_x1:c_x2);
segment = imgaussfilt(image(b_y1:b_y2,b_x1:b_x2), 3);
image = imgaussfilt(image, 5);
image(b_y1:b_y2, b_x1:b_x2) = segment;
image(c_y1:c_y2, c_x1:c_x2) = focus;
blurred_image = image;
end

Answers (1)

Pranjal Kaura
Pranjal Kaura on 27 Jul 2021
Edited: Pranjal Kaura on 27 Jul 2021
Hey,
It is my understanding that you want to develop a function using which you can focus on a certain part of the image by blurring other parts. You want to blur the image in segments to achieve a gradient look in the output.
The problem with your code is
  1. You’re using a 3D image with imgausfilt function, which is a 2D image smoothing function. If you want to use 3D images, you can use imgausfilt3 function.
  2. The extracted co-ordinates (c_y1, c_y2) are incorrect and [b_x1, b_y1] had overhead calculations which can be optimised.
Here’s a code snippet that might be helpful
img = rgb2gray(imread('img.jpeg'));
imshow(img);
title("Input Image");
figure;
out_img = blurImage(img, 0.55, 0.1, 0.85, 0.8);
imshow(out_img);
title("Output Image");
function blurred_image = blurImage(image, x1, y1, x2, y2)
[height, width, ~] = size(image);
%desired area coordinates
c_x1 = floor(width * x1);
c_y1 = floor(height * y1);
c_x2 = floor(width * x2);
c_y2 = floor(height * y2);
%blurred segment
b_x1 = floor(abs(c_x1/2));
b_y1 = floor(abs(c_y1/2));
b_x2 = floor(abs(c_x2 + (width - c_x2)/2));
b_y2 = floor(abs(c_y2 + (height - c_y2)/2));
fprintf("%d %d\n", b_x1, c_x1);
fprintf("%d %d\n", b_y1, c_y1);
fprintf("%d %d\n", b_x2, c_x2);
fprintf("%d %d\n", b_y2, c_y2);
focus = image(c_y1:c_y2, c_x1:c_x2);
segment = imgaussfilt(image(b_y1:b_y2,b_x1:b_x2), 5);
image = imgaussfilt(image, 9);
image(b_y1:b_y2, b_x1:b_x2) = segment;
image(c_y1:c_y2, c_x1:c_x2) = focus;
blurred_image = image;
end

Products

Community Treasure Hunt

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

Start Hunting!