# Image Processing for measuring

Hello, very new to the coding world, started last week learning, and have been searching around for a simple solution to my problem but can’t find anything specific.
Here is my situation: My end goal is to have an app that takes a picture of for example a tree cross-section (from a specified distance) and can both measure the diameter, but also count the rings. Now one step at a time, and my first task is measuring (assuming that will be the easier of the two).
I have made it through Dan’s tutorials, and used some of his examples with brightness thresholds, and have gotten it so that only the cross-section is seen in black and white. I’m trying to translate my thoughts to code but it isn’t going over very well, so here are my thoughts:
If I could set x to 0, y to height/2, and at each pixel determine if it is different than the last going from left to right, and then print out that x value, and do that for the opposite side. After I have those two x values it shouldn’t be too hard to subtract the two, and now I have my diameter in pixels, and soon I’ll know what the ratio is of a pixel. I’ve messed around with for and if stuff, but the syntax is my bottleneck right now being very new to the game.

Absolutely any input is much appreciated.
Also I don’t know how to “link” to my code so you can easily run it, but I’ll gladly paste it down below:

``````PImage crossSection;

void setup() {
size(900, 800);
}

void draw() {
display();
for (int x = 0; x < width-1; x++) {
for (int y = 0; y < height; y++) {
int loc1 = x    + y*width;
int loc2 = (x+1)+ y*width;
float b1 = brightness(crossSection.pixels[loc1]);
float b2 = brightness(crossSection.pixels[loc2]);
//Threshold to 20
float diff = abs(b1-b2);
if (diff > 20) {
pixels[loc1] = color(0);
} else {
pixels[loc1] = color(255);
}
}
}
updatePixels();
}

void display() {
image(crossSection, 0, 0);
}
``````

hallo, welcome
best would be if we can play with your concept / code,

-a- for this it is better to post ( repair above ) as code using the

``````</> code tag ( Preformatted text )
``````

from the post edit window menu
and paste your code into the
```
type or paste code here
```

-b- also you might want to share your picture file.

-c- is the picture the cross section and you want analyze it ( the whole surface )
or you want to define ( any ) line over that picture
and only check ?color? along that line?

Done, and thanks for the tip.

I think I understand you question, here goes:
If possible, yes, make a line going across the center, and analyze those pixels along that line. The idea would be to have the line, and start from left to right checking if the pixel changed, if it did then save that x coordinate, and do the same on the right side.
Also I originally said cross section of a tree, but I’m actually messing around with cross sections of cabbage, similar but people would understand the former more.
Thanks for your help, let me know with any other questions/suggestions.

1 Like

My main question is, how would I go about making that line, and checking for changes in the pixels?
Thanks

that is a little bit poor question, but i think i know what you talk about

• a- with former info you might need a center point ( to know right and left )
• b- would it be only a horizontal line ( you want to check on )? or any angle?
• c- interactivity, think you want the user to define ? by mouse click ? that center and angle of the line?

in that case you start
-2 on first mouse click remember that mouse position as center point ( and draw a little circle )
-3 on second mouse click remember the angle between first and second point ( to draw the line )
-4 calc like PVector
p0 at ( 0,y0)
p1 at ( width,y1 )
( or even more complicated if you allow lines what hit y = 0 or y = height )
-5 on ( key == ‘r’ ) reset that settings ( incl the 2 click counter ) and allow to do it again.

for getting the color of a pixel

or faster version

anyhow you need a for loop what asks for the pixel along that line ( only )
defined ( -4 ) PVectors in regards to the center point.

Got it! Thank you for your help. I mainly referenced the get() function and just researched from there mostly because I don’t have the vocabulary to understand a lot of the info you provided haha.

``````PImage crossSection;

//size 1/4 of iPhone image
void setup() {
size(1008, 756);
}

void draw() {
display();
crossSection.filter(THRESHOLD, 0.1);
updatePixels();
}

void display() {
image(crossSection, 0, 0);
crossSection.resize(1008, 756);
}

void mouseClicked() {
//j variable to stop loop
int j = 0;
//i variable as x coordinate
int i = 0;
while (j<1) {
//setting the color of the pixel to leftSide variable
color leftSide = get(i, height/2);
//if the color is white, continue, if not back out of loop
if (leftSide != -1) {
j++;
} else {
i++;
}
}
int p = 0;
int r = width-1;
while (p<1) {
color rightSide = get(r, height/2);
if (rightSide != -1) {
//ratio of iPhones pixels to inches
println((r-i)*3.5/196 +" inches");
p++;
} else {
r--;
}
}
}
``````

I was able to successfully size my camera down to the hundreth of an inch being 6.55 inches. This only works holding an iPhone X at 15 inches from the object using the ratio that I found.

Nice man! You absolutely nailed it. Welcome to the forum!

1 Like