Determine the age of the tree by the number of circles
Hello! Can i determine the age of the tree by the number of circles?
Merry Christmas. Here's your present... The ring count that I get is 70. Make sure to mark my answer as correct, if you find that it works for you. :)
Here is a cropped version of your input image, which contains only the necessary data:
Here is the C++ code to count the rings:
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
#include <iostream>
using namespace std;
int main(void)
{
Mat frame = imread("rings_slice.png");
if (frame.empty())
{
cout << "Error loading image file" << endl;
return -1;
}
cvtColor(frame, frame, CV_BGR2GRAY);
adaptiveThreshold(frame, frame, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
// Use centre column
int column_index = frame.cols / 2;
int ring_count = 0;
// Start with the second row
for (int i = 1; i < frame.rows; i++)
{
// If this pixel is white and the previous pixel is black
if (255 == frame.at<unsigned char>(i, column_index) && 0 == frame.at<unsigned char>(i - 1, column_index))
ring_count++;
}
cout << ring_count << endl;
return 1;
}
Alternatively, here is the Python code:
import numpy as np
import cv2
import sys
frame = cv2.imread("rings_slice.png")
if frame is None:
print('Error loading image')
exit()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.adaptiveThreshold(frame, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
rows = frame.shape[0]
cols = frame.shape[1]
# Use centre column
column_index = cols / 2
ring_count = 0;
# Start with the second row
for i in range(1, rows):
# If this pixel is white and the previous pixel is black
if 255 == frame[i, column_index] and 0 == frame[i - 1, column_index]:
ring_count += 1;
print ring_count
I wrote a C++ code that scans for rings in both horizontal and vertical mode... the code selects the highest ring count:
https://github.com/sjhalayka/opencv_r...
Making the same changes to the Python code should be fairly easy.
Anyway, the code is picking up too many rings. You'll have to experiment with the threshold()
/adaptiveThreshold()
parameters for each wood type, I'd think. Have fun!
Asked: 2017-12-25 04:30:31 -0600
Seen: 1,485 times
Last updated: Dec 25 '17