필터 지우기
필터 지우기

How to add a high-speed test in a self-defined FAST feature detector function?

조회 수: 2 (최근 30일)
Hi all,
I want to define a FAST detector which do a high-speed test on 4 pixel first, if 3 or more past test then test all 16 pixels to find a feature points. (Without using detectFASTFeatures function.)
The orgional FAST detector is this, where white points are feature points:
Sample1_double = im2double(Sample1);
Sample1_temp = my_fast_detector_no_highS(Sample1);
Sample1_mat = Sample1_double + Sample1_temp;
function fastM = my_fast_detector_no_highS(img)
n = 12;
threshold = 10;
img = rgb2gray(img);
img_temp = zeros(756,606);
img_temp(4:753,4:603) = img;
fastM = zeros(750,600);
intensityD = zeros(750,600,16);
intensityD(:,:,1) = abs(img_temp(1:end-6,4:end-3)-img_temp(4:end-3,4:end-3));
intensityD(:,:,5) = abs(img_temp(4:end-3,7:end)-img_temp(4:end-3,4:end-3));
intensityD(:,:,9) = abs(img_temp(7:end,4:end-3)-img_temp(4:end-3,4:end-3));
intensityD(:,:,13) = abs(img_temp(4:end-3,1:end-6)-img_temp(4:end-3,4:end-3));
intensityD(:,:,2) = abs(img_temp(1:end-6,5:end-2)-img_temp(4:end-3,4:end-3));
intensityD(:,:,3) = abs(img_temp(2:end-5,6:end-1)-img_temp(4:end-3,4:end-3));
intensityD(:,:,4) = abs(img_temp(3:end-4,7:end)-img_temp(4:end-3,4:end-3));
intensityD(:,:,6) = abs(img_temp(5:end-2,7:end)-img_temp(4:end-3,4:end-3));
intensityD(:,:,7) = abs(img_temp(6:end-1,6:end-1)-img_temp(4:end-3,4:end-3));
intensityD(:,:,8) = abs(img_temp(7:end,5:end-2)-img_temp(4:end-3,4:end-3));
intensityD(:,:,10) = abs(img_temp(7:end,3:end-4)-img_temp(4:end-3,4:end-3));
intensityD(:,:,11) = abs(img_temp(6:end-1,2:end-5)-img_temp(4:end-3,4:end-3));
intensityD(:,:,12) = abs(img_temp(5:end-2,1:end-6)-img_temp(4:end-3,4:end-3));
intensityD(:,:,14) = abs(img_temp(3:end-4,1:end-6)-img_temp(4:end-3,4:end-3));
intensityD(:,:,15) = abs(img_temp(2:end-5,2:end-5)-img_temp(4:end-3,4:end-3));
intensityD(:,:,16) = abs(img_temp(1:end-6,3:end-4)-img_temp(4:end-3,4:end-3));
intensityD = intensityD > threshold;
fastM = sum(intensityD, 3);
fastM = fastM > n;
end
I want to add a count matrix that if high_speed_count(Xi,Yi)>2, then keep calculate intensity difference sorrund img_temp(Xi,Yi); else if high_speed_count(Xi,Yi)<=2, skip to img_temp's next element and do FAST for next pixel.
I modify the upper function as follows:
function fastM = my_fast_detector(img)
n = 12;
threshold = 10;
img = rgb2gray(img);
img_temp = zeros(756,606);
img_temp(4:753,4:603) = img;
fastM = zeros(750,600);
intensityD = zeros(750,600,16);
high_speed_count = zeros(750,600);
intensityD(:,:,1) = abs(img_temp(1:end-6,4:end-3)-img_temp(4:end-3,4:end-3));
if intensityD(:,:,1) > threshold
high_speed_count(:,:) = high_speed_count(:,:) + 1;
end
intensityD(:,:,5) = abs(img_temp(4:end-3,7:end)-img_temp(4:end-3,4:end-3));
if intensityD(:,:,5) > threshold
high_speed_count(:,:) = high_speed_count(:,:) + 1;
end
intensityD(:,:,9) = abs(img_temp(7:end,4:end-3)-img_temp(4:end-3,4:end-3));
if intensityD(:,:,9) > threshold
high_speed_count(:,:) = high_speed_count(:,:) + 1;
end
intensityD(:,:,13) = abs(img_temp(4:end-3,1:end-6)-img_temp(4:end-3,4:end-3));
if intensityD(:,:,13) > threshold
high_speed_count(:,:) = high_speed_count(:,:) + 1;
end
if high_speed_count(:,:) > 2
intensityD(:,:,2) = abs(img_temp(1:end-6,5:end-2)-img_temp(4:end-3,4:end-3));
intensityD(:,:,3) = abs(img_temp(2:end-5,6:end-1)-img_temp(4:end-3,4:end-3));
intensityD(:,:,4) = abs(img_temp(3:end-4,7:end)-img_temp(4:end-3,4:end-3));
intensityD(:,:,6) = abs(img_temp(5:end-2,7:end)-img_temp(4:end-3,4:end-3));
intensityD(:,:,7) = abs(img_temp(6:end-1,6:end-1)-img_temp(4:end-3,4:end-3));
intensityD(:,:,8) = abs(img_temp(7:end,5:end-2)-img_temp(4:end-3,4:end-3));
intensityD(:,:,10) = abs(img_temp(7:end,3:end-4)-img_temp(4:end-3,4:end-3));
intensityD(:,:,11) = abs(img_temp(6:end-1,2:end-5)-img_temp(4:end-3,4:end-3));
intensityD(:,:,12) = abs(img_temp(5:end-2,1:end-6)-img_temp(4:end-3,4:end-3));
intensityD(:,:,14) = abs(img_temp(3:end-4,1:end-6)-img_temp(4:end-3,4:end-3));
intensityD(:,:,15) = abs(img_temp(2:end-5,2:end-5)-img_temp(4:end-3,4:end-3));
intensityD(:,:,16) = abs(img_temp(1:end-6,3:end-4)-img_temp(4:end-3,4:end-3));
intensityD = intensityD > threshold;
fastM = sum(intensityD, 3);
fastM = fastM > n;
end
end
After I apply it on origional image:
Sample1_double = im2double(Sample1);
Sample1_temp = my_fast_detector(Sample1);
Sample1_mat = Sample1_double + Sample1_temp;
it shows no change.
I don't know where is the issue.
May I ask help to fix this?
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2023년 10월 18일
if intensityD(:,:,1) > threshold
high_speed_count(:,:) = high_speed_count(:,:) + 1;
end
That code is equivalent to
if all(intensityD(:,:,1) > threshold, 'all')
high_speed_count = high_speed_count + 1;
end
except for some differences in memory management.
That is, testing an array of data with if or while is considered to succeed only if all of the values being tested are non-zero. If there is even one location intensityD(J,K,1) <= threshold then the entire if will be considered to be false.
Each time you operate on high_speed_count you are operating on the entire matrix, adding 1 to every entry. So if it happened that one of the tests succeeded then you would be incrementing all of the matrix from 0 to 1.
What you were probably thinking you were doing was asking MATLAB to locate the places in intensityD that were > threshold, and increment the corresponding locations in high_speed_count by 1. But that is not at all what you programmed.
You should learn how to use what MATLAB calls "logical indexing" https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
  댓글 수: 5
Walter Roberson
Walter Roberson 2023년 10월 18일
It would be possible to refine the code further so that the operation such as
abs(img_temp(1:end-6,3:end-4)-img_temp(4:end-3,4:end-3))
was only done for the locations of interest, instead of the current method where it is done for all locations after which we select out the locations we want.
However, it turns out that a lot of the time, selecting out the desired locations and doing arithmetic only on them, can be slower than doing the calculation on everything and then selected the desired parts of it. This is because the logical selection operation has to be done on each of the arrays involved, giving a vector result, then the calculation is done on the vectors... which can be slower than just tossing a block of memory into a parallelized multi-threaded internal operation (that might, for example, divide up the rows to process one set of rows per core.)
WENQING CAO
WENQING CAO 2023년 10월 18일
I kind of got what you mean, in matlab, matrix operation is faster than a loop because of it's shift operation; however if run it in other language then the operation on a vertor is faster.
C-family is able to do the shift operation and faster than a loop, however, I am so afread of memory leaking and making mistake on pointers, I avoid use C-family as much as possible. ε=(´ο`*)))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by