Code for png image using sobel operator

cc = imread('CocaCola.png');
cc = imread('CocaCola.png');
cc = uint8(cc);
figure, imshow(cc); title('cc');
cc = rgb2gray(cc);
cc = double(cc);
edg = edgy(cc);
% Pre-allocate the filtered_image matrix with zeros
edg = zeros(size(cc));
Mx = [-1 0 1; -2 0 2; -1 0 1];
My = [-1 -2 -1; 0 0 0; 1 2 1];
for i = 1:size(cc, 1) - 2
for j = 1:size(cc, 2) - 2
% Gradient approximations
Gx = sum(sum(Mx.*cc(i:i+2, j:j+2)));
Gy = sum(sum(My.*cc(i:i+2, j:j+2)));
% Calculate magnitude of vector
edg(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);
end
end
edg = uint8(edg);
figure, imshow(edg); title('edg');
thresholdValue = 100; % varies between [0 255]
output_image = max(edg, thresholdValue);
output_image(output_image == round(thresholdValue)) = 0;
output_image = im2bw(output_image);
figure, imshow(output_image); title('Edge Detected Image');
I wrote this code but did't work .please help

回答 (3 件)

Fatih Yurekli
Fatih Yurekli 2020 年 10 月 16 日
編集済み: Walter Roberson 2020 年 12 月 27 日

2 投票

function [c3] = edgy(c)
s1 = [-1 0 1;-2 0 2;-1 0 1];
s1 = double(s1);
s2 = [1 2 1; 0 0 0;-1 -2 -1];
s2 = double(s2);
c = double(c);
[M N] = size(c);
for i = 1:M-2
for j = 1:N-2
a = c(i:i+2, j:j+2);
a1 = a.*s1;
a2 = a.*s2;
b1= 0;
b2= 0;
for k = 1:3
for l = 1:3
b1 = b1+a1(k,l);
b2 = b2+a2(k,l);
end
end
c1(i,j) =b1;
c2(i,j) = b2;
end
end
c3 = sqrt(c1.^2+c2.^2);
c3 = uint8(c3);
end

14 件のコメント

ABHIJIT BISWAS
ABHIJIT BISWAS 2020 年 12 月 23 日
works fine for me. Thank you.
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
c3 is not working bro in 28th line
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
Do you mean the uint8() line? What error message are you encountering?
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
Undefined function 'unit8' for input arguments of type 'double'.
Error in edgy (line 28)
c3= unit8(c3)
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
i used the same program given above but it showing error in line 28 please help me
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
you mistyped as unit8 instead of uint8
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
after all the program it says like this as incorrect value help me
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
you are required to assign to a variable named output . Not output_image
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
can you please give me a code im confused at all please help me brother
PRASANTH R
PRASANTH R 2020 年 12 月 27 日
mr.walter please answer my question
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
wrong:
output_image = im2bw(output_image);
Better:
output = im2bw(output_image);
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
Note: it is 7am my time and I have been up all night doing research and answering questions. I am in bed, assisting multiple people at the same time, and hoping to fall asleep.
Image Analyst
Image Analyst 2020 年 12 月 27 日
Prashanth, you know you can click the "Copy code" icon in the upper right of the code blocks here in answers. You don't have to re-type all this in to your MATLAB, which could introduce typos.

サインインしてコメントする。

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 9 月 7 日
編集済み: Sulaymon Eshkabilov 2020 年 9 月 7 日

0 投票

Hi,
Here is the simple solution with MATLAB's builtin function of Image Processing Toolbox that works quite well:
COCA = imread('CC.png');
BW1 = edge(COCA,'sobel');
figure;
imshow(BW1)
title('Sobel Filter');

2 件のコメント

Mohaiminul Islam
Mohaiminul Islam 2020 年 9 月 7 日
didn,t work . could you please write code according to the question given above.
Mohaiminul Islam
Mohaiminul Islam 2020 年 9 月 7 日

サインインしてコメントする。

Génesis Vivas
Génesis Vivas 2020 年 9 月 24 日
編集済み: Génesis Vivas 2020 年 9 月 24 日

0 投票

Hi friend, I think that your problem is that you started in for i = 1:size(cc, 1) - 2, you shouldnt take the border, i=1, what you should do is start from the second position both in the rows and in the columns, and stake out the sums Mx and My, and done!
for i = 2:rows-1
for j = 2:columns-1

1 件のコメント

Image Analyst
Image Analyst 2020 年 12 月 27 日
His original indexing code would have had no problem - it never went outside the image. Though most people would have done it like you did. The real problem (and what threw the error) was that he did this:
edg = edgy(cc);
without ever having written edgy function and either saving that in a file on the path (like the current folder), or having it defined down below in his script. You can't call a function if it's not defined and it has to be defined either
  1. in the same script, with the "function" keyword down below the script part of the file, OR
  2. in a separate m-file.

サインインしてコメントする。

製品

リリース

R2019a

質問済み:

2020 年 9 月 7 日

コメント済み:

2020 年 12 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by