#include"cv.h"
#include "bmpproperty.h"
#include <iostream>
#include<opencv2\opencv.hpp>
#include<math.h>
using namespace std;
using namespace cv;
void main(){
Mat M = imread("dip2gray.jpg");
Mat grayim(M.rows, M.cols, CV_8UC1);
int count = 1; int c2 = count;
for (int i = 0; i < M.rows; ++i)
{
for (int j = 0; j < M.cols; ++j){
grayim.at<uchar>(i, j) = M.at<Vec3b>(i, j)[1];
if (grayim.at<uchar>(i, j) < 66)
{
grayim.at<uchar>(i, j) = 0;
}
else grayim.at<uchar>(i, j) = 1;
}
}//二值化
Mat I3(grayim.rows-2, grayim.cols-5, CV_8UC1);
for (int i2 = 1; i2 < M.rows - 2; ++i2){
for (int j2 = 3; j2 < M.cols - 3; ++j2){
I3.at<uchar>(i2, j2 - 2) = grayim.at<uchar>(i2, j2);
}
}
//去边框
Mat label(I3.rows, I3.cols, CV_8UC1);
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
label.at<uchar>(m, n) = 0;
}
}
int vl[200]; int counter1[200];
for (int m = 1; m < 200; ++m){
vl[m] = 0; counter1[m] = 0;
}
int vl0; int p = 1;
int m = 1;
for (int n = 1; n < I3.cols; ++n){
if (I3.at<uchar>(m, n) == 0){
if (I3.at<uchar>(m, n - 1) == 1){
count = count + 1;
label.at<uchar>(m, n) = count;
}
if (I3.at<uchar>(m, n - 1) == 0){
label.at<uchar>(m, n) = label.at<uchar>(m, n - 1);
}
}
}
for (int m = 2; m < I3.rows ; ++m){
for (int n = 1; n < I3.cols ; ++n){
if (I3.at<uchar>(m, n) == 0){
if (I3.at<uchar>(m - 1, n) == 1 && I3.at<uchar>(m, n - 1) == 1){//case1
count = count + 1;
label.at<uchar>(m, n) = count;
}
if (I3.at<uchar>(m - 1, n) == 0 && I3.at<uchar>(m, n - 1) == 1){//case2
label.at<uchar>(m, n) = label.at<uchar>(m - 1, n);
}
if (I3.at<uchar>(m - 1, n) == 1 && I3.at<uchar>(m, n - 1) == 0){//case3
label.at<uchar>(m, n) = label.at<uchar>(m, n - 1);
}
if (I3.at<uchar>(m - 1, n) == 0 && I3.at<uchar>(m, n - 1) == 0){//case4
label.at<uchar>(m, n) = label.at<uchar>(m, n - 1);
if (label.at<uchar>(m, n - 1) != label.at<uchar>(m - 1, n)){
if (vl[label.at<uchar>(m, n)] == 0){
if (vl[label.at<uchar>(m - 1, n)] == 0){
vl[label.at<uchar>(m, n)] = label.at<uchar>(m - 1, n);
}
else
vl[label.at<uchar>(m, n)] = vl[label.at<uchar>(m - 1, n)];
}
else{
if (vl[label.at<uchar>(m - 1, n)] == 0){
vl[label.at<uchar>(m - 1, n)] = vl[label.at<uchar>(m, n)];
}
else{
vl0 = vl[label.at<uchar>(m - 1, n)];
for (p = 1; p <count; ++p){
if (vl[p] == vl0)
vl[p] = vl[label.at<uchar>(m, n)];
}
}
}
}
}
}
}
}imshow("test", label); waitKey(0);
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (label.at<uchar>(m, n) != 0 && vl[label.at<uchar>(m, n)] != 0){
label.at<uchar>(m, n) = vl[label.at<uchar>(m, n)];
}
else
if (label.at<uchar>(m, n) != 0 && vl[label.at<uchar>(m, n)] == 0){
vl[label.at<uchar>(m, n)] = label.at<uchar>(m, n);
}
else label.at<uchar>(m, n) = 0;
}
}
//标记完毕
int vl2[200]; int c1 = 0; int counter[200];
for (int m = 1; m < 200; ++m){
vl2[m] = 0; counter[m] = 0;
}
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (label.at<uchar>(m, n)){
counter[label.at<uchar>(m, n) + 1] = counter[label.at<uchar>(m, n) + 1] + 1;
}
}
}
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (I3.at<uchar>(m, n) == 0){
if ((counter[label.at<uchar>(m, n) + 1]) < 3){//滤除噪声
I3.at<uchar>(m, n) = 1;
}
}
}
}
//对滤波后的图像赋标志值
Mat label1(I3.rows, I3.cols, CV_8UC1);
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (I3.at<uchar>(m, n) == 0 && label.at<uchar>(m, n) != 0)
label1.at<uchar>(m, n) = label.at<uchar>(m, n);
else label1.at<uchar>(m, n) = 0;
}
}
//计数函数
int v1 = 1;
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (label1.at<uchar>(m, n) != 0){
c1 = 0;
for (int v2 = 1; v2 <= v1; v2++){
if (label1.at<uchar>(m, n) != vl2[v2]){
c1 = c1 + 1;
}
}
if (v1 == c1){
vl2[v1] = label1.at<uchar>(m, n);
v1 = v1 + 1;
}
}
}
}
//上色函数
int num = v1 - 1;
Mat label2(I3.rows, I3.cols, CV_8UC3);
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
if (label1.at<uchar>(m, n) != 0){
label2.at<Vec3b>(m, n)[0] = floor(label1.at<uchar>(m, n)*1.4);
label2.at<Vec3b>(m, n)[1] = floor(abs(76 - label1.at<uchar>(m, n))*3.2);
label2.at<Vec3b>(m, n)[2] = floor((192 - label1.at<uchar>(m, n))*1.5);
}
else{
label2.at<Vec3b>(m, n)[0] = 153;
label2.at<Vec3b>(m, n)[1] = 248;
label2.at<Vec3b>(m, n)[2] = 127;
}
}
}
//滤波边框加粗
Mat I4(I3.rows, I3.cols, CV_8UC1);
for (int m = 1; m < I3.rows; ++m){
for (int n = 1; n < I3.cols; ++n){
I4.at<uchar>(m, n) = I3.at<uchar>(m, n);
}
}
for (int m = 1; m < I3.rows - 1; ++m){
for (int n = 1; n < I3.cols - 1; ++n){
if (I3.at<uchar>(m + 1, n) == 0 || I3.at<uchar>(m, n + 1) == 0){
I4.at<uchar>(m , n) = 0;
}
}
}
imshow("grayim", grayim * 255);
imshow("cut", I3 * 255);
imshow("color", label2);
imshow("boundary", I4*255);
cout <<"Number="<< num<<endl;
cout << "Counter=" << endl;
for (int i = 1; i < 200; ++i){
cout << counter[i] <<" ";
}
waitKey(0);
}
细胞识别Region labeling and counting
最新推荐文章于 2022-07-04 19:21:42 发布