微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在MATLAB中应用修复算法时,无法计算图像中给定点的梯度

如何解决在MATLAB中应用修复算法时,无法计算图像中给定点的梯度

我正在尝试实现Criminisi(https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-2003-84.pdf)的研究论文中的修复算法。所以我不能同时使用MATLAB的内置函数。我停留在无法获得正确结果的步骤,因此无法继续进行。我无法正确计算的术语是这个

enter image description here

enter image description here

初始化代码

originalImage = imread("fig 1.jpg");
mask = imread("fig 1 target.jpg"); % white represent target region

% Converting original image and mask to grayscale
[row,column,channel] = size(originalImage);
% converting to grayscale images
if channel > 1
    I = rgb2gray(originalImage);
else
    I = originalImage;
end

[~,~,channel] = size(mask);
if channel > 1
    mask = rgb2gray(mask);
end

% Finding edges on mask which will 
% return bounded boundary
maskBoundary = edge(mask,'Canny');

% Finding boundary points of target region
[boundaryY,boundaryX] = find(maskBoundary);

% Binary mask of target(Omega) and source(Phi) region
omega = imbinarize(mask,0.1);
phi = ~omega;

% source region
source = originalImage.*uint8(phi);

% Edges on source image
sourceEdges = edge(I,'canny').*phi;
% imshow(sourceEdges);
% Magnitude and direction of source image
[sMag,sDir] = imgradient(rgb2gray(source),'sobel');
% Gradient in x and y direction
[sGx,sGy] = imgradientxy(rgb2gray(source));

% Confidence matrix
% source pixels confidence = 1
% target pixels confidence = 0 (initially)
confidence = double(phi);

% defining the patch size
texelSize = 9;
texelSize_half = (texelSize-1)/2;

% Confidence and data term along target boundary
confidence_i = zeros(size(boundaryX));
data_i = zeros(size(boundaryX));
% unitVector normal to target region boundary
unitVector_i = zeros(2,1);

要在其中计算条件项的循环

for index=2:size(boundaryY,1)-1
    Confidence_val = 0.0;
    Grad_val = 0.0;
    
    % confidence calculation
    confidence_i(index) = sum( ...
        confidence(boundaryY(index)-texelSize_half:boundaryY(index)+texelSize_half,...
        boundaryX(index)-texelSize_half:boundaryX(index)+texelSize_half),'all')/texelSize^2;
    
    i = index;
    px = boundaryX(i); % column
    py = boundaryY(i); % row
    % gradient vector (rotated) at point p(px,py)
    gradPx = -sGy(py,px);
    gradPy = sGx(py,px);
    
    
    % Unit matrix normal to target region boundary
    tangent = [boundaryX(i+1)-boundaryX(i-1); boundaryY(i+1)-boundaryY(i-1)];
    vectorMod = sqrt((boundaryX(i+1)-boundaryX(i-1))^2 + (boundaryY(i+1)-boundaryY(i-1))^2);
    
    unitVector_i(1) = -tangent(2)/vectorMod;
    unitVector_i(2) = tangent(1)/vectorMod;
    nPx = unitVector_i(1);
    nPy = unitVector_i(2);
    
    data_i(i) = abs(dot([gradPx,gradPy],[nPx,nPy]))/255;
end

我在图像处理方面经验不足,因此我对某些术语不太了解。有人可以解决这个问题,还是请我参考其MATLAB / OpenCV实现。


我的代码的当前状态在此处,包括示例图片

https://github.com/lkdhruw/inpainting_exercise


在GitHub上找到了此实现,这对我来说有些困难,并且在我的MATLAB 2020b上也不起作用

https://github.com/ikuwow/inpainting_criminisi2004


我已经检查了以下链接

What is an Image Gradient?

Image gradient in the point

Calculating image gradient

image gradient angle computation

Patch priority and its effect on Criminsi's Exemplar Based Inpainting


编辑

问题

enter image description here

我在计算数据项D(p)时遇到问题。参见标记为1的图像,只有一个强度变化,因此根据算法,优先级项P(p) = C(p) D(p)在该位置应有一个峰。问题在于有多个峰彼此之间相距太远。我只提到了数据项(图标签3),因为输出与优先级项(图标签4)非常相似。有人可以找出我是否正确计算了D(p)吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。