यह छवि प्रसंस्करण होमवर्क में हमारा अभ्यास है। मेरा कोड ठीक काम कर रहा है। मैं कोड अनुकूलन के साथ कुछ मदद प्राप्त करना चाहता हूं।matlab में 2 डी रूपांतरण - कोड अनुकूलन
function C = convolve_slow(A,B)
(file name is accordingly convolve_slow.m)
This routine performs convolution between an image A and a mask B.
Input: A - a grayscale image (values in [0,255])
B - a grayscale image (values in [0,255]) serves as a mask in the convolution.
Output: C - a grayscale image (values in [0,255]) - the output of the convolution.
C is the same size as A.
Method: Convolve A with mask B using zero padding. Assume the origin of B is at
floor(size(B)/2)+1.
Do NOT use matlab convolution routines (conv,conv2,filter2 etc).
Make the routine as efficient as possible: Restrict usage of for loops which are expensive (use matrix multiplications and matlab routines such as dot etc).
To simplify and reduce ifs, you should pad the image with zeros before starting your convolution loop.
Do not assume the size of A nor B (B might actually be larger than A sometimes).
यहाँ हमारे समाधान
function [ C ] = convolve_slow(A,B)
%This routine performs convolution between an image A and a mask B.
% Input: A - a grayscale image (values in [0,255])
% B - a grayscale image (values in [0,255]) serves as a mask in the convolution.
% Output: C - a grayscale image (values in [0,255]) - the output of the convolution.
% C is the same size as A.
%
% Method: Convolve A with mask B using zero padding. Assume the origin of B is at floor(size(B)/2)+1.
% init C to size A with zeros
C = zeros(size(A));
% make b xy-reflection and vector
vectB = reshape(flipdim(flipdim(B,1),2)' ,[] , 1);
% padding A with zeros
paddedA = padarray(A, [floor(size(B,1)/2) floor(size(B,2)/2)]);
% Loop over A matrix:
for i = 1:size(A,1)
for j = 1:size(A,2)
startAi = i;
finishAi = i + size(B,1) - 1;
startAj = j;
finishAj = j + size(B,2) - 1;
vectPaddedA = reshape(paddedA(startAi :finishAi,startAj:finishAj)',1,[]);
C(i,j) = vectPaddedA* vectB;
end
end
end
के बाद से मैं इमेज प्रोसेसिंग और Matlab के लिए नया हूँ है। क्या आप कोड अनुकूलन के साथ विशेष रूप से मैट्रिक्स आधारित संचालन के साथ मेरी मदद कर सकते हैं। क्या लूप का उपयोग नहीं करना संभव है?
आप fft2 और ifft2 उपयोग करने के लिए अनुमति दी जाती है किसी भी तरह से काम करता है? यदि ऐसा है तो यह एक बहुत तेज़ समाधान पाने का एक तरीका होगा। – MarkV
नहीं, खेद नहीं है, मुख्य अवधारणा यह है कि हम यह सीखने के लिए एक दृढ़ संकल्प लागू करते हैं कि यह कैसे काम करता है। अच्छा यह काम करता है! अब मैं देखना चाहता हूं कि इसे बेहतर तरीके से करने का कोई तरीका है या नहीं। – Gilad