Ask Your Question
1

Determine the age of the tree by the number of circles

asked 2017-12-25 04:30:31 -0600

drom gravatar image

updated 2017-12-25 04:45:17 -0600

Hello! Can i determine the age of the tree by the number of circles? image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-25 09:58:12 -0600

sjhalayka gravatar image

updated 2017-12-25 14:29:15 -0600

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:

image description

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
edit flag offensive delete link more

Comments

sjhalayka, thank you for answer. For this image, the answer is 64. image description

But for this image the answer is 7. image description

drom gravatar imagedrom ( 2017-12-29 05:18:54 -0600 )edit

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!

sjhalayka gravatar imagesjhalayka ( 2017-12-29 15:16:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-25 04:30:31 -0600

Seen: 1,471 times

Last updated: Dec 25 '17