發表文章

目前顯示的是 2月, 2022的文章

Matlab影像處理(五)邊緣偵測Sobel濾波

圖片
 %% sobel邊緣濾波 close all; clear; clc; img = double(rgb2gray(imread('Color_lena512.bmp'))); [row, col] = size(img); Hck = [-1 -2 -1; 0 0 0; 1 2 1;]; Vck = [-1 0 1; -2 0 2; -1 0 1;]; Hfeature = zeros(row, col); Vfeature = zeros(row, col); addfeature = zeros(row, col); H=0; V=0; s=zeros(1, row.*col); for i=1:row-2     for j=1:col-2         for ii=1:3             for jj=1:3                 H = H + img(i+ii-1, j+jj-1).*Hck(ii, jj);                 V = V + img(i+ii-1, j+jj-1).*Vck(ii, jj);             end         end         Hfeature(i+1, j+1) = H;         Vfeature(i+1, j+1) = V;         H=0; V=0;     end end count = 0; for i=1:row     for j=1:col         count = count + 1;         addfeature(i, j) = abs(Hfeature(i, j)) + abs(Vfeature(i, j));         s(1, count) = addfeature(i, j);     end end s=sort(s); min=s(1, 1); max=s(1, row.*col); outputimg = zeros(row, col); n =@(x) ( (x-min)./(max-min) ).*255; for i=1:row     for j=1:col         outputimg(i, j) = n(addfeature(i, j));     end e

Matlab影像處理(四)影像濾波

圖片
 %% 平均濾波 中值濾波 close all; clear; clc; img = double(rgb2gray(imread('A.jpg'))); [row, col] = size(img(:, :, 1)); avgimg = zeros(row, col); medimg = zeros(row, col); count=0; A=0; s=zeros(1, 9); for i=1:row-2     for j=1:col-2         for ii=1:3             for jj=1:3                 count = count + 1;                 A = A + img(i+ii-1, j+jj-1);                 s(1, count) = img(i+ii-1, j+jj-1);             end         end         avgimg(i+1, j+1) = A./count;         count=0; A=0;         s = sort(s);         medimg(i+1, j+1) = s(1, 5);         s = zeros(1, 9);     end end subplot(1, 3, 1), imshow(uint8(img)), title('原圖') subplot(1, 3, 2), imshow(uint8(avgimg)), title('平均濾波') subplot(1, 3, 3), imshow(uint8(medimg)), title('中值濾波')

Matlab影像處理(三)膚色偵測

圖片
%%膚色偵測 close all; clear; clc; img = double(imread('img.jpg')); [row, col] = size(img(:, :, 1)); outputimg = zeros(row, col); for i=1:row     for j=1:col         if img(i, j, 1)>img(i, j, 2) & img(i, j, 2)>img(i, j, 3) & img(i, j, 1)-img(i, j, 2)>=5             outputimg(i, j) = 255;         end     end end subplot(1, 2, 1), imshow(uint8(img)), title('原圖') subplot(1, 2, 2), imshow(uint8(outputimg)), title('處理後圖')

Matlab影像處理(二)背景相減

圖片
%%影像相減 close all; clear; clc; img = double(imread('img.jpg')); bgimg = double(imread('backGround.jpg')); [row, col] = size(img(:, :, 1)); outputimg  = zeros(row, col); binaryimg = zeros(row, col); T=10; for i=1:row     for j=1:col         outputimg(i, j) = (( bgimg(i, j, 1)-img(i, j, 1) ).^2 + ( bgimg(i, j, 2)-img(i, j, 2) ).^2 + ( bgimg(i, j, 2)-img(i, j, 2) ).^2).^0.5;         if outputimg(i, j) > T             binaryimg(i, j) = 255;         end     end end subplot(2, 2, 1), imshow(uint8(img)), title('原圖') subplot(2, 2, 2), imshow(uint8(bgimg)), title('BG圖') subplot(2, 2, 3), imshow(uint8(outputimg)), title('相減後') subplot(2, 2, 4), imshow(uint8(binaryimg)), title('二值化')

Matlab影像處理(一)直方圖

圖片
 %% 直方圖 close all; clear; clc; fprintf('影像直方圖\n\n') img=imread('Color_lena512.bmp'); Graybar = zeros(1, 256); Rbar = zeros(1, 256); Gbar = zeros(1, 256); Bbar = zeros(1, 256); [row, col] = size(img(:, :, 1)); grayimg = rgb2gray(uint8(img)); for i=1:row     for j=1:col         A = grayimg(i ,j) + 1;         Graybar(1, A) = Graybar(1, A) + 1;                  R = img(i, j, 1) + 1;         Rbar(1, R) = Rbar(1, R) + 1;                  G = img(i, j, 2) + 1;         Gbar(1, G) = Gbar(1, G) + 1;                  B = img(i, j, 3) + 1;         Bbar(1, B) = Rbar(1, B) + 1;        end end subplot(2, 2, 1), bar(Graybar, 'k'), title('灰階直方圖') subplot(2, 2, 2), bar(Rbar, 'r'), title('R直方圖') subplot(2, 2, 3), bar(Gbar, 'g'), title('G直方圖') subplot(2, 2, 4), bar(Bbar, 'b'), title('B直方圖') %% 等化法 close all; clear; clc; img = imread('Color_lena512.bmp'); Grayimg = rgb2gray(img); [row, col] = size(img(:, :, 1)); outputi