图像处理算法,基于opencv的一些有用的函数

对于工作上的一些积累,避免重复造轮子,记录一些自己在开发过程中的一些函数

//求src和base之间的差值
void myDiff(Mat src,Mat base, Mat &diff)
{
    diff = src.clone();
    for (int x = 0; x < src.cols; x++)
    {
        for (int y = 0; y < src.rows; y++)
        {
            int data1 = src.at<unsigned char>(y, x);
            int data2 = base.at<unsigned char>(y, x);
            int dif = 0;
            if (data1 > data2)
            {
                dif=data1 - data2;
            }
            else
            {
                dif = 0;
            }
            diff.at<unsigned char>(y, x) = dif;
        }
    }
}

//对图像求线平均值,code=0,按列求取;code=1,按行求取
void avelineblur(Mat& img,int code)
{
    int nrow = img.rows; int ncol = img.cols;
    if(0 == code)
    for (int i = 0; i < img.cols; i++)
    {
        int rowsum = 0;
        for (int j = 0; j < nrow; j++)
        {
            int value = img.at<uchar>(j, i);
            rowsum = rowsum + value;
        }
        int avepixel = rowsum / nrow;
        for (int k = 0; k < nrow; k++)
        {
            img.at<uchar>(k, i) = avepixel;
        }
    }
    if (1 == code)
    {
        for (int i = 0; i < img.rows; i++)
        {
            int colsum = 0;
            for (int j = 0; j < ncol; j++)
            {
                int value = img.at<uchar>(i, j);
                colsum = colsum + value;
            }
            int avepixel = colsum / ncol;
            for (int k = 0; k < ncol; k++)
            {
                img.at<uchar>(i, k) = avepixel;
            }
        }
    }
}

 

//找背景像素,arr是直方图统计矩阵,minb为预设最小背景,maxb为预设最大背景
int find_background(MatND arr,const int minb,const int maxb)
{
    int maxV,max_index;
    max_index = 0;
    maxV = 0;
    for (int i = minb; i <= maxb; i++)
    {
        int t = (int)arr.at<float>(i, 0);
        if (t > maxV)
        {
            maxV = t;
            max_index = i;
        }    
    }
    return max_index;
}

//灰度图像直方图统计
MatND getHistogramImage(Mat image) {
    //compute histogram first  
    Mat src(image);
    if (src.channels() == 3)
        cvtColor(src, src, CV_RGB2GRAY);
    cv::MatND hist;
    int hist_size[] = { 256 };
    float hranges[] = { 0,256 };
    float sranges[] = { 0,256 };
    const float* ranges[] = { hranges, sranges };
    int channels[] = { 0 };
    calcHist(&src, 1, channels, Mat(), hist, 1, hist_size, ranges, true);
    return hist;
}

 

//找到轮廓点中离screenEdge边缘较近的点,距离阈值为threDis,结果存为res
void contourMaxMinXY(vector<Point> contour,vector<Point>& res,const int* screeEdge,int threDis)
{
    if (contour.empty())exit(1);
    if (1 == contour.size()) {
        res.push_back(contour.at(0)); return;
    }
    for (int i = 0; i < contour.size(); i++)
    {
        int difMinX = abs(contour.at(i).x - screeEdge[0]), difMaxX = abs(contour.at(i).x - screeEdge[1]), difMinY = abs(contour.at(i).y - screeEdge[2]), difMaxY = abs(contour.at(i).y - screeEdge[3]);
        if (difMinX < threDis && contour.at(i).x > screeEdge[0] && contour.at(i).x < screeEdge[1] && contour.at(i).y > screeEdge[2] && contour.at(i).y < screeEdge[3])res.push_back(contour.at(i));
        if (difMaxX < threDis && contour.at(i).x > screeEdge[0] && contour.at(i).x < screeEdge[1] && contour.at(i).y > screeEdge[2] && contour.at(i).y < screeEdge[3])res.push_back(contour.at(i));
        if (difMinY < threDis && contour.at(i).x > screeEdge[0] && contour.at(i).x < screeEdge[1] && contour.at(i).y > screeEdge[2] && contour.at(i).y < screeEdge[3])res.push_back(contour.at(i));
        if (difMaxY < threDis && contour.at(i).x > screeEdge[0] && contour.at(i).x < screeEdge[1] && contour.at(i).y > screeEdge[2] && contour.at(i).y < screeEdge[3])res.push_back(contour.at(i));
    }
}

//求取二维点集内任意两点距离的最大值
double maxdistance(vector<Point> con)
{
    double dis = 0;
    for (int i = 0; i < con.size()-1; i++)
    {
        Point p1(con.at(i));
        for (int j = i + 1; j < con.size(); j++)
        {
            Point p2(con.at(j));
            double dist = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
            if (dis < dist)dis = dist;
        }
    }
    return dis;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值