# 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? edit retag close merge delete

Sort by » oldest newest most voted 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)
{

if (frame.empty())
{
return -1;
}

cvtColor(frame, frame, CV_BGR2GRAY);

// Use centre column
int column_index = frame.cols / 2;

int ring_count = 0;

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

if frame is None:
exit()

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

rows = frame.shape
cols = frame.shape

# Use centre column
column_index = cols / 2

ring_count = 0;

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

more

sjhalayka, thank you for answer. For this image, the answer is 64. But for this image the answer is 7. 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!

Official site

GitHub

Wiki

Documentation