matlab和opencv对图像做差结果不吻合

白泥于泥鳅 2017-12-25 10:00:34
两张图片做差提取白色区域,之前用matlab写了一下感觉没啥问题,效果也理想,最近开始学opencv,就像也实现一下matlab做过的这个功能,但是遇到了一些问题,还望大神解答!感觉是opencv图片储存方式不能直接做差吗?
f1图片

bg图片

matlab处理结果

opencv处理结果


matlab代码
bg=imread('bg.jpg');
f1=imread('f1.jpg');
bg=rgb2gray(bg);
f1=rgb2gray(f1);
distance=f1-bg;
imshow(distance);

opencv代码
import cv2
import numpy as np
bg=cv2.imread("bg.JPG")
f1=cv2.imread("f1.JPG")
bg=cv2.cvtColor(bg,cv2.COLOR_BGR2GRAY)
f1=cv2.cvtColor(f1,cv2.COLOR_BGR2GRAY)
distance=bg-f1
cv2.namedWindow('test', cv2.WINDOW_NORMAL)
cv2.imshow("test",distance)
cv2.waitKey(0)
...全文
907 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33655799 2017-12-27
  • 打赏
  • 举报
回复
出现这种问题我觉得应该是转灰度图像时两张图的相同部分像素点灰度值不一样造成的,你可以对做过差的图像再来一次二值化。
worldy 2017-12-26
  • 打赏
  • 举报
回复
opencv的Mat好像没有重载operator - ()函数,应该不是opencv的问题吧,你用纯opencv看看
赵4老师 2017-12-25
  • 打赏
  • 举报
回复
仅供参考:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_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 temp;
    CvMat tmp;

    img.convertTo(temp, CV_32FC1, 1.0/255.0, 0.0);
    tmp=temp;
    cvPow(&tmp, &tmp, gamma);
    temp.convertTo(dst , CV_8UC1 , 255.0    , 0.0);
}
int main() {
    cvNamedWindow("display",1);
    img=imread("image.jpg",1);
    r.x=img.cols/10;
    r.y=img.rows/3;
    r.width=img.cols*8/10;
    r.height=img.rows*2/3;
    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 Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
    erode(bw,bw,Structure0,Point(-1,-1));
    Mat Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
    dilate(bw,bw,Structure1, 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(maxrect),br(brect);
                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;
}
潇潇雨云 2017-12-25
  • 打赏
  • 举报
回复
matlab 给设置误差了,opencv的应该是准确的

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧