图片像素对比OpenCV实现,实现人工分割跟算法分割图像结果的对比

简介: 图片对比,计算不同像素个数,已经比率。实现人工分割跟算法分割图像结果的对比,但是只能用灰度图像作为输入   // imageMaskComparison.cpp : 定义控制台应用程序的入口点。

图片对比,计算不同像素个数,已经比率。实现人工分割跟算法分割图像结果的对比,但是只能用灰度图像作为输入

 

// imageMaskComparison.cpp : 定义控制台应用程序的入口点。
//

// imageMaskComparison.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{


	String image_name,image1,image2;
	cout<<"input Parameters:"<<endl;

	cout<<"image name 1 : ";
	cin>>image1;
	cout<<"image name 2 : ";
	cin>>image2;


	Mat img1 = imread((char *)image1.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
	Mat img2 = imread((char *)image2.c_str(), CV_LOAD_IMAGE_GRAYSCALE);//两幅图像的大小需要一致 
	imshow("img1",img1);
	imshow("img2",img2);

	Mat img_result1 , img_result2 , img_result ;

	img_result1.create(img1.rows,img1.cols,CV_8UC1);
	img_result1 = 255;

	img_result2.create(2,img1.size,CV_8UC1);
	img_result2 = 255;

	img_result.create(img2.rows,img2.cols,CV_8UC1);
	img_result = 0;

	//subtract(img1,img2, img_result1);
	//subtract(img2,img1, img_result2);
	//add(img_result1, img_result2, img_result1);

	absdiff(img1,img2,img_result);

	int init_index = 0;

	
	/*
	for(int i=0; i<img_result.rows; i++)
	{
		for(int j=0; j<img_result.cols; j++) 
		{
			if (img_result.at<uchar>(i,j)== 255)
			{
				init_index++;
			}
			
			
			
		}
	}
	



	*/
	

	int count = 0;
	

	for(int i=0; i<img1.rows; i++)
	{
		
		
		for(int j=0; j<img1.cols; j++) 
		{
			
			
			if ( img1.at<uchar>(i,j) == img2.at<uchar>(i,j))
			{
				
				img_result.at<uchar>(i,j) = (uchar)255;
			}
			else
			{
				count++;
				img_result.at<uchar>(i,j) = (uchar)0;
			}

		}
	}
	

	int sum = img1.cols*img1.rows;
	double error_ratio = count/(double)sum;
	
	//cout<<init_index<<endl;
	cout<<"number of different pixel:"<<count<<endl;
	cout<<"error ratio: "<<error_ratio<<endl;
	imwrite("result.bmp",img_result);

	//imshow("result1", img_result1);
	//imshow("result2", img_result2);
	 namedWindow( "result", CV_WINDOW_AUTOSIZE );
	imshow("result", img_result);
	waitKey();
	return 0;
}


 

 

 

相关文章
|
3天前
|
机器学习/深度学习 传感器 算法
【OpenCV】告别人工目检:深度学习技术引领工业品缺陷检测新时代
【OpenCV】告别人工目检:深度学习技术引领工业品缺陷检测新时代
|
11天前
|
算法 TensorFlow 算法框架/工具
基于直方图的图像阈值计算和分割算法FPGA实现,包含tb测试文件和MATLAB辅助验证
这是一个关于图像处理的算法实现摘要,主要包括四部分:展示了四张算法运行的效果图;提到了使用的软件版本为VIVADO 2019.2和matlab 2022a;介绍了算法理论,即基于直方图的图像阈值分割,通过灰度直方图分布选取阈值来区分图像区域;并提供了部分Verilog代码,该代码读取图像数据,进行处理,并输出结果到&quot;result.txt&quot;以供MATLAB显示图像分割效果。
|
12天前
|
前端开发 API 计算机视觉
opencv绘制基本图形,在图片上添加文字
opencv绘制基本图形,在图片上添加文字
|
12天前
|
计算机视觉 Python
轻松掌握opencv的8种图像变换
轻松掌握opencv的8种图像变换
|
13天前
|
算法 计算机视觉
【OpenCV】- 图像修复
【OpenCV】- 图像修复
|
13天前
|
Serverless 计算机视觉
【OpenCV】-图像的矩
【OpenCV】-图像的矩
|
13天前
|
编解码 物联网 计算机视觉
【OpenCV】—图像金子塔与图片尺寸缩放
【OpenCV】—图像金子塔与图片尺寸缩放
|
13天前
|
前端开发 计算机视觉 C++
【OpenCV】—分离颜色通道、多通道图像混合
【OpenCV】—分离颜色通道、多通道图像混合
|
13天前
|
API 计算机视觉
【OpenCV】—图像对比度、亮度值调整
【OpenCV】—图像对比度、亮度值调整
|
13天前
|
计算机视觉 索引
【OpenCV】—ROI区域图像叠加&图像混合
【OpenCV】—ROI区域图像叠加&图像混合