种子区域与opencv生长 [英] Seeded region growing with opencv
本文介绍了种子区域与opencv生长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要选择一个像素值,并应用根据种子像素生长的区域。在尝试写代码后,结果总是一个黑色的图像
无论我使用什么种子点。整个问题涉及 GrowColor
函数。我的猜想是与ifs的逻辑错误。
#include< cv.h>
#include< highgui.h>
using namespace std;
int xDim,yDim,zDim;
float ThreshHold = 45.0;
unsigned long total [3];
int coont,tt;
IplImage * Image1;
IplImage * Image2;
CvScalar s = cvScalar(0,0,0,0);
CvScalar s11 = cvScalar(0,0,0,0)
int Diff,mean [3],temp [3];
void GrowColor(int x,int y);
int main(int argc,char * argv []){
char value [4];
int pixType,dimCut;
int Dbug = false;
int Xseed = 40,Yseed = 234;
int i = 0,x,y;
Image1 = cvLoadImage(lenah.jpg);
yDim = Image1-> height;
xDim = Image1-> width;
// int step = Image1-> widthStep;
// uchar * data =(uchar *)Image1> imageData;
//新图片
Image2 = cvCreateImage(cvSize(Image1-> width,Image1-> height),IPL_DEPTH_8U,
1);
cvZero(Image2);
total [0] = total [1] = total [2] = coont = 0;
//处理
for(y = Yseed-5; y <= Yseed + 5; y ++)
for(x = Xseed - 5; x < = Xseed + 5; x ++)
if((x> 0)&&(y> 0)&&(x< xDim)&& )){
coont ++;
s = cvGet2D(Image1,x,y);
total [0] + = abs(s.val [0]);
total [1] + = abs(s.val [1]);
total [2] + = abs(s.val [2]);
}
GrowColor(Xseed,Yseed);
cvNamedWindow(wndname,1);
cvShowImage(original,Image1);
cvShowImage(wndname,Image2);
cvWaitKey(0);
return 0;
}
void GrowColor(int x,int y){
//检查点是否已经是区域的一部分
s.val [0] = 0;
s.val [1] = 0;
s.val [2] = 0;
s.val [3] = 0;
if((x <1)&&(y <1))
s = cvGet2D(Image2,x,y)
if(s.val [0] == 0){
int k;
if((x == 1)&&(y == 1))
s11 = cvGet2D(Image1,x,y)
mean [0] = total [0] / coont;
mean [1] = total [1] / coont;
mean [2] = total [2] / coont;
temp [0] = abs(s11.val [0]) - mean [0];
temp [1] = abs(s11.val [1]) - mean [1];
temp [2] = abs(s11.val [2]) - mean [2];
Diff =
(int)(sqrt(
(temp [0] * temp [0] + temp [1] * temp [1]
+ temp [2] * temp [2])/ 3));
if(Diff< ThreshHold){
total [0] + = abs(s11.val [0]);
total [1] + = abs(s11.val [1]);
total [2] + = abs(s11.val [2]);
coont ++;
s.val [0] = 120;
if((x> 0)&&(y> 0))
cvSet2D(Image2,x,y,s)
if(x> 2)
GrowColor(x-1,y);
if(y> 2)
GrowColor(x,y-1);
if(x< xDim - 2)
GrowColor(x + 1,y);
if(y< yDim - 2)
GrowColor(x,y + 1);
}
}
}
c> c> c>递归函数可能会导致无限循环。检查该函数中的代码一次。
I need to select a pixel value and apply the region growing in terms of the seed pixel. After trying to write the code, the result was always a black image
regardless of what seed point I used. The whole problem is involved in the GrowColor
function. My guess is a logical error with the ifs.
#include <cv.h>
#include <highgui.h>
using namespace std;
int xDim, yDim, zDim;
float ThreshHold = 45.0;
unsigned long total[3];
int coont, tt;
IplImage *Image1;
IplImage *Image2;
CvScalar s = cvScalar(0, 0, 0, 0);
CvScalar s11 = cvScalar(0, 0, 0, 0);
int Diff, mean[3], temp[3];
void GrowColor(int x, int y);
int main(int argc, char *argv[]) {
char value[4];
int pixType, dimCut;
int Dbug = false;
int Xseed = 40, Yseed = 234;
int i = 0, x, y;
Image1 = cvLoadImage("lenah.jpg");
yDim = Image1->height;
xDim = Image1->width;
// int step= Image1->widthStep;
//uchar* data = (uchar *)Image1->imageData;
//New image
Image2 = cvCreateImage(cvSize(Image1->width, Image1->height), IPL_DEPTH_8U,
1);
cvZero(Image2);
total[0] = total[1] = total[2] = coont = 0;
//Process
for (y = Yseed - 5; y <= Yseed + 5; y++)
for (x = Xseed - 5; x <= Xseed + 5; x++)
if ((x > 0) && (y > 0) && (x < xDim) && (y < yDim)) {
coont++;
s = cvGet2D(Image1, x, y);
total[0] += abs(s.val[0]);
total[1] += abs(s.val[1]);
total[2] += abs(s.val[2]);
}
GrowColor(Xseed, Yseed);
cvNamedWindow("wndname", 1);
cvShowImage("original", Image1);
cvShowImage("wndname", Image2);
cvWaitKey(0);
return 0;
}
void GrowColor(int x, int y) {
//Check to see if point already part of region
s.val[0] = 0;
s.val[1] = 0;
s.val[2] = 0;
s.val[3] = 0;
if ((x < 1) && (y < 1))
s = cvGet2D(Image2, x, y);
if (s.val[0] == 0) {
int k;
if ((x == 1) && (y == 1))
s11 = cvGet2D(Image1, x, y);
mean[0] = total[0] / coont;
mean[1] = total[1] / coont;
mean[2] = total[2] / coont;
temp[0] = abs(s11.val[0]) - mean[0];
temp[1] = abs(s11.val[1]) - mean[1];
temp[2] = abs(s11.val[2]) - mean[2];
Diff =
(int) (sqrt(
(temp[0] * temp[0] + temp[1] * temp[1]
+ temp[2] * temp[2]) / 3));
if (Diff < ThreshHold) {
total[0] += abs(s11.val[0]);
total[1] += abs(s11.val[1]);
total[2] += abs(s11.val[2]);
coont++;
s.val[0] = 120;
if ((x > 0) && (y > 0))
cvSet2D(Image2, x, y, s);
if (x > 2)
GrowColor(x - 1, y);
if (y > 2)
GrowColor(x, y - 1);
if (x < xDim - 2)
GrowColor(x + 1, y);
if (y < yDim - 2)
GrowColor(x, y + 1);
}
}
}
解决方案
Making GrowColor
a recursive function may result in an infinite loop. Check the code in that function once.
这篇关于种子区域与opencv生长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文