Code Monkey home page Code Monkey logo

nut_cnt's Introduction

螺母计数

HUSTAIA 2022 FALL

结果

原图,效果图:

test

控制台结果输出( $\gamma=141,e=6,T_d=1500,T_u=4200$ ):

ours sibling:15
315 346 829 1923 1933 1952 1999 2046 2895 2983 2984 3003 3054 3139 3200
nut num: 12

算法

算法流程:

  1. 以阈值 $\gamma$ 二值化
  2. $e$ 为核大小进行腐蚀
  3. 寻找图像中所有的内轮廓并计算面积
  4. 依据下阈值 $T_d$ 和上阈值 $T_u$ 筛选内轮廓面积进行计数

注:

  • 实验过程中注意细致调整相机光圈,焦距,底座亮度,二值化阈值,腐蚀核大小,面积下阈值和上阈值,这些对实验结果都会造成影响
    • 相机视域内不要出现底座边框
    • 调整焦距光圈使成像清晰
    • 二值化阈值默认值可以设为150左右
    • 腐蚀核大小不要过小,默认值可以设为6左右
    • 面积下阈值和上阈值依据控制台输出的当前实验条件下的所有内轮廓面积大小进行选取(不同相机距离、腐蚀核大小等条件下螺母内轮廓大小并不一样)
  • 基本就是调参,参数并不是很好调,可能需要消耗些时间
  • 在结果图像中将以灰色显示出螺母内轮廓
  • 如果可以的话,尽可能减少粘连,比较强的粘连实在无法处理(本算法理论上如果全部是螺母的话不受粘连影响,但是实际上如果豆子之间粘连过强的话还是会受影响)

实现细节

这个只是一个简单的调用了opencv的测试demo,后续还需自行整合,图像处理主函数见opencv_test.cpp

contour函数改自FreshJesh5/Suzuki-Algorithm (github.com),具体见contour.cpp

简易版腐蚀(可以用在自己的代码中):

/*
param:
 nut_bImage
 k: kernel size
author: polowitty
*/
void erode(MVImage& nut_bImage, int k)
{
	int num_row = nut_bImage.GetHeight();
	int num_col = nut_bImage.GetWidth();
	UCHAR* b_p = (UCHAR*)nut_bImage.GetBits();//获取二值图内存地址
	UCHAR minV;
	UCHAR pixel_v;
	for (int i = 0; i < num_row; i++)
	{
		for (int j = 0; j < num_col; j++)
		{
			minV = 255;
			for (int xi = i; xi <= i + k; xi++)
			{
				for (int yi = j; yi <= j + k; yi++)
				{
					if (xi<0 || xi>=num_row || yi < 0 || yi >= num_col) continue;

					pixel_v = *(b_p + xi * num_col + yi);
					minV = min(minV, pixel_v);
				}
			}
			*(b_p + i * num_col + j) = minV;
		}
	}
}

nut_cnt's People

Contributors

polowitty avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.