import cv2
import glob
def count_white_particles(image):
# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 对图像进行二值化
_, thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)
# 去除噪声
thresh = cv2.medianBlur(thresh, 5)
# 查找图像中的轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制颗粒并计算数量
count = 0
for contour in contours:
area = cv2.contourArea(contour)
if area > 135: # 过滤掉过小的颗粒
count += 1
return count
img_paths = glob.glob("D:\bysj\*.jpg")
for img_path in img_paths:
# 读取图片
img = cv2.imread(img_path)
# 计算白色颗粒数量
count = count_white_particles(img)
# 显示结果
cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.imshow('Image', img)
cv2.putText(img, "Number of white particles: " + str(count), (10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.waitKey(0)
# 输出颗粒数量
print("Number of white particles in", img_path, ":", count)
不知道你这个问题是否已经解决, 如果还没有解决的话:练习涉及到的方法主要是Sobel(src,ddepth,dx,dy,ksize),其中ddepth表示图像的深度,dx和dy分别表示水平和竖直方向,ksize是Sobel算子的大小。练习时分别对图像进行了水平方向和竖直方向的检测,改变算子大小对图像进行对比,还比较了不同算子之间的差异。
具体练习结果截图如下。
滴!改变算子大小的图像对比:
滴!不同算子之间的差异对比:
另外,针对canny算子进行了更深入的学习。
具体练习结果截图如下。
滴!边缘检测canny算子不同参数对比:
仅供参考,尽管是C++:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2\highgui\highgui_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
CV_RGB( 0, 0,128),
CV_RGB( 0,128, 0),
CV_RGB( 0,128,128),
CV_RGB(128, 0, 0),
CV_RGB(128, 0,128),
CV_RGB(128,128, 0),
CV_RGB(128,128,128),
CV_RGB(160,160,160),
CV_RGB( 0, 0,255),
CV_RGB( 0,255, 0),
CV_RGB( 0,255,255),
CV_RGB(255, 0, 0),
CV_RGB(255, 0,255),
CV_RGB(255,255, 0),
CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
Mat tmp;
img.convertTo(tmp, CV_32FC1, 1.0/255.0, 0.0);
pow(tmp, gamma, tmp);
tmp.convertTo(dst , CV_8UC1, 255.0, 0.0);
}
int main() {
cvNamedWindow("display",1);
img=imread("image.jpg",1);
r.x =img.cols*1/10;
r.y =img.rows*1/5;
r.width =img.cols*8/10;
r.height=img.rows*3/5;
smallImg=img(r);
cvtColor(smallImg,gray,CV_BGR2GRAY);
// medianBlur(gray,gray,5);
equalizeHist(gray,gray);
gamma_correct(gray,gray,4.0);
imshow("display",gray);
waitKey(0);
bw=(gray>threshval);
imshow("display",bw);
waitKey(0);
// Mat Structure1=getStructuringElement(MORPH_RECT ,Size(6,6));
Mat Structure1=getStructuringElement(MORPH_ELLIPSE,Size(6,6));
dilate(bw,bw,Structure1, Point(-1,-1));
imshow("display",bw);
waitKey(0);
Mat Structure0=getStructuringElement(MORPH_RECT ,Size(3,3));
Mat Structure0=getStructuringElement(MORPH_ELLIPSE,Size(3,3));
erode(bw,bw,Structure0,Point(-1,-1));
imshow("display",bw);
waitKey(0);
findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
if (!contours.empty()&&!hierarchy.empty()) {
idx=0;
n=0;
vector<Point> approx;
for (;idx>=0;idx=hierarchy[idx][0]) {
color=colors[idx%15];
// drawContours(smallImg,contours,idx,color,1,8,hierarchy);
approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
const Point* p = &approx[0];
int m=(int)approx.size();
polylines(smallImg, &p, &m, 1, true, color);
circle(smallImg,Point(p[0].x,p[0].y),3,color);
circle(smallImg,Point(p[1].x,p[1].y),2,color);
for (int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
n++;
if (1==n) {
maxrect=boundingRect(Mat(contours[idx]));
} else {
brect=boundingRect(Mat(contours[idx]));
CvRect mr;
mr.x=maxrect.x;
mr.y=maxrect.y;
mr.width=maxrect.width;
mr.height=maxrect.height;
CvRect br;
br.x=brect.x;
br.y=brect.y;
br.width=brect.width;
br.height=brect.height;
maxrect=cvMaxRect(&mr,&br);
}
}
circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
}
imshow("display",smallImg);
waitKey(0);
cvDestroyWindow("display");
return 0;
}