在三通道彩图和单通道灰度图中画矩形框

        今天学习了如何在彩图中和单通道灰度图中画矩形框。由于没有直接用到opencv中的函数,所有代码都是由自己编写的。之所以自己写代码,不直接用函数,是想到后面如果要移植到DSP中,也要自己写函数。(附上代码)


//自己写的画矩形程序
void drawRectangle(IplImage* src, CvPoint point1, CvPoint point2)   
{
    //最下边
int step = src->widthStep/sizeof(unsigned char);
int channels = src->nChannels;
uchar *data = (uchar *)src->imageData;


if (src->nChannels == 1)
{
//灰度图画上边
uchar* ptr = (uchar *)src->imageData + point1.y*src->width;  
for (size_t x=point1.x; x {
ptr[x] = 255;
}
//灰度图画下边
ptr = (uchar *)src->imageData + point2.y*src->width;
 for (x=point1.x; x {
ptr[x] = 255;
}
//灰度图画左右两边
 for (size_t y=point1.y; y  {
 data[y*step + point1.x] = 255;
 data[y*step + point2.x] = 255;
 }
}
 

//画彩图
     if (src->nChannels == 3)
{
for (size_t x=point1.x; x {
data[point1.y*step + x*channels] = 0;
data[point1.y*step + x*channels + 1] = 255;
data[point1.y*step + x*channels + 2] = 0;
}
     
// 画下边
for ( x=point1.x; x {
data[point2.y*step + x*channels] = 0;
data[point2.y*step + x*channels + 1] = 255;
data[point2.y*step + x*channels + 2] = 0;
}


//画左边
for (size_t y = point1.y; y {
data[y*step + point1.x*channels] = 0;
data[y*step + point1.x*channels+1] = 255;
data[y*step + point1.x*channels+2] = 0;

}


  //画右边
for ( y = point1.y; y {
data[y*step + point2.x*channels] = 0;
data[y*step + point2.x*channels+1] = 255;
data[y*step + point2.x*channels+2] = 0;

}
}


    return;
}

你可能感兴趣的:(opencv学习笔记,opencv,画矩形)