Calculate the Cone angle of a spray image
17 views (last 30 days)
Show older comments
Hi, I'm having some problems in calculating the cone angle for an array of diesel sprays. i tried initially using the bwtraceboundary function, however, this has problems in analysing multiple images. At the very start of the spray development it does not work(when the spray is very small) and in the late stages (when the injector has finished spraying, the spray is large, in contact with the far wall and no longer in contact with the nozzle)it also does not work.
Below is one of the original images and its cropped and thresholded version.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158317/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158319/image.png)
after re-sizing and thresholding the image, the matlab code i used was as follows:
dim=size(BW);
%left edge
col1=56;
row1=min(find(BW(:,col1), 1 ));
%right edge
col2=63;
row2=min(find(BW(col2,:), 1 ));
boundary1=bwtraceboundary(BW,[row1,col1],'N',8,220,'counter');
boundary2=bwtraceboundary(BW,[row2,col2],'N',8,220);
% fit points to line
ab1=polyfit(boundary1(:,2),boundary1(:,1),1);
ab2=polyfit(boundary2(:,2),boundary2(:,1),1);
r = ab1(1) .* boundary1(:,2) + ab1(2)+offsetY;
% now plot both the points in y and the curve fit in r onto original image using an offset
hold on;
plot(offsetX+boundary1(:,2), r, 'g','LineWidth',2);
r = ab2(1) .* boundary2(:,2) + ab2(2)+offsetY;
hold on;
plot(offsetX+boundary2(:,2), r, 'g','LineWidth',2);
% create vectors
vect1=[1 ab1(1)];
vect2=[1 ab2(1)];
dp=dot(vect1,vect2);
length1=sqrt(sum(vect1.^2));
length2=sqrt(sum(vect2.^2));
% obtain angle
angle=180-acos(dp/(length1*length2))*180/pi;
%This was then plotted on top of the original sized image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158321/image.png)
However, I am sure there are many other far more accurate and consistent ways of doing this. My knowledge of matlab is very poor, i have used tutorials up until now, but there don't seem to be any more that can help. Although I know that a good way of doing this is by scanning the image in a certain direction and finding the boundary between white and black pixels, unfortunately i do not know how to write any of this in code.
Any help would be really appreciated!
Thank you very much
Jed
1 Comment
지영 허
on 15 Jul 2021
Hello Jed
I'm so happy to find the code you made. I almost succeeded in getting the spray cone angle through the code you made. However, it is difficult to complete, so I would like to practice with your picture and code, so could you tell me all of your codes? I hope there is a part like offsetX or offsetY missing. I'd really appreciate it if you could let me know.
Answers (3)
Image Analyst
on 10 Mar 2014
No, you don't want to use bwtraceboundary. You can use bwboundaries instead if you can isolate the plume and get rid of the surrounding circle so you have a nice binary mask of just the plume/spray.
Assuming you've identified the apex of the triangle at the top, then the spray angle changes depending on how far down you want to measure it. And it will be different if you just look at a line and use that width, or if you do a fit from all the side points from the current line back up to the origin point. So do you want the "instantaneous" width plotted at a function of line down from the origin, or do you want the "fitted width"? If you go for the instantaneous width, then I guess you could just get the widest angle. If you get the fit, then you need to decide where to stop the fit because it starts to taper down again near the bottom.
7 Comments
Image Analyst
on 11 Mar 2014
Maybe try to have your "x" direction be the direction along the plume and the "y" direction be the cross direction. Maybe it doesn't like having 2 Y for the same x. If you want, attach a script that gets the binary image of the plume and then does your code for finding edges and fitting.
Sean de Wolski
on 12 Mar 2014
Edited: Sean de Wolski
on 12 Mar 2014
Here's the approach I would take. Segment the plume, sum up the rows, calculate the slope of the rows up to the peaks, polyfit that.
With your original image being '2011.jpg'
%%Load, green slice
I = imread('2011.jpg');
green = I(:,:,2);
%%Find plume
P = (im2bw(green,graythresh(green)));
P = xor(P,imfill(P,'holes'));
P = keepMaxObj(P);
% imshow(P)
%%Engine
npx = sum(P,2); % sum along second dimension
npx = nonzeros(npx); % extract nonzeros
[~,idx] = max(npx);
npx = npx(1:idx);
plot(npx);
mb = polyfit((1:idx).',npx,1); % fit line
slope = mb(1);
the_angle = atand(slope)
6 Comments
bimal khatiwada
on 9 Aug 2020
Edited: Image Analyst
on 9 Aug 2020
Hi everyone,
Can you please tell me the MATLAB code to calculate spray length and spray cone angle for any images? For example:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/343606/image.jpeg)
1 Comment
Image Analyst
on 9 Aug 2020
See Also
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!