Why is blockproc giving errors ?
显示 更早的评论
I am trying to do a block processing operation but it keeps on giving me errors on running the code. Please help me.
采纳的回答
Change following lines in you code function getAvgGradient().
1. blksize is a 1x2 array. zeros() can only accept a scalar as input
GxData = zeros(blksize(1)^2, 1);
GyData = zeros(blksize(1)^2, 1);
GsxData = zeros(blksize(1)^2, 1);
GsyData = zeros(blksize(1)^2, 1);
2. In MATLAB, the index start from 1
for row = 1:size(mag, 1) % change from 0 to 1
for col = 1: size(mag, 2) % change from 0 to 1
16 个评论
Thank you so much, it worked, I had forgotten Matlab starts from 1.
Please how can I get two values from the block processing function instead if one?
I want the function I am passing to it to return two values. How can I do that?
There is no built-in way for blockproc() to output multiple values. You may need to write your own loop if you want to do something like that. In simple cases, you can manipulate the function to output multiple values. For example, here, the block proc is outputting the mean and maximum value from each block. But It will require you to separate both outputs manually, and it will only work if both outputs have the same dimensions.
M = reshape(1:16,4,4);
R = blockproc(M, [2 2], @(x) [mean(x.data, 'all') max(x.data, [], 'all')]);
R1 = R(:,1:2:end);
R2 = R(:,2:2:end);
Fego Etese
2020-4-26
编辑:Fego Etese
2020-4-26
At this moment I am calling blockproc twice to get the second value, i created a similare function and just outputed the different values in the two functions, so calling blockproc twice. But now i am having issues with the second function.
Please take a look at it here.
The getAvgAngle gives me errors
Thanks
I don't get any error, both function run correctly. Can you also share the image you are using? Maybe it is something related to that.
This is the error I am getting. I have also include the mat file of the image.

I get a different error message, but that is probably you are using R2018a. I am using R2020a. The reason seems to be this. For some blocks, all the pixels are while and when you run these lines
[mag, dir] = imgradient(block_struct.data, 'sobel');
mag are all zeros. And then the function proceed and when reach these lines
avgGsx = Gxx - Gyy;
avgGsy = 2 * Gxy;
both avgGsx and avgGsy are zero. The following if condition become true and then on this line
phi = atand(avgGsy/avgGsx);
you get a 0/0 situation which output NaN. => phi=NaN. So at the end
if(phi <= 0)
theta = phi + (180/2);
elseif(phi > 0)
theta = phi - (180/2);
end
none of these conditions become true, and theta does not get any value. So when the function ends, the blockproc complains that it does not return any value of theta. You need to correct your logic accordingly.
Fego Etese
2020-4-26
编辑:Fego Etese
2020-4-26
I really apprecaite this. Thanks a lot. I don't think i could've figured this out on my own.
I'll fix it now
The easiest way to debug such issues is to add breakpoint to your function: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html and run it line by line. It helps in figuring out the issue.
The thing is I didn't even know the error was coming from the function i wrote. I thought it came from the blockproc function. I didn't understand the error message well.
Thank you, i wil use the debugging function from now on.
I am glad to be of help.
Yes, the error message is not much clear in your MATLAB version. It was quite clear in R2020a.
It's finally working wothout errors. i used the sum function to check if mag was all zeros as a condition to continue processing
Thanks.
Maybe i should get the version 2020a, will all my codes i have writen earlier in the 2018a still work in the version 2020a?
Most probably, everything will work. Mathworks will likely not drop the support for any function suddenly.
Fego Etese
2020-4-26
编辑:Fego Etese
2020-4-26
And also please i need your help with the quiver function. I want to visualize the magnitude and angle data i have gotten from the block processing with arrows in each block just like the (b) image below. Please how can i do that?
I can open a new question if you prefer to answer there.

Ameer Hamza
2020-4-26
编辑:Ameer Hamza
2020-4-26
Yes, you can start a new question. This is not related to the current question, so any discussion here cannot be fruitful for anyone else in the future. You can comment the link of the question so that i will get a notification.
I have started a new question, here is the link
thanks
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Neighborhood and Block Processing 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
