金字塔压力-面积-位移图像识别 外接矩形绘制(wcy)

金字塔压力-面积-位移图像识别 外接矩形绘制(wcy)

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
	cv::Mat image = cv::imread("E://image//3.jpg");//载入图像
	cv::Mat imageGray;
	cvtColor(image, imageGray, COLOR_BGR2GRAY); //转换为灰度图像
	//IplImage* img = cvLoadImage("E://image//1.png");
	if (imageGray.empty())
	{
		std::cout << "read image failure" << std::endl;
		return -1;
	}

/*RNG rng((unsigned)time(NULL)); //生成随机数
	Mat Canny_output;
	vector<vector<Point>> contours; //边缘矢量
	vector<Vec4i> hierarchy;   //形状因子
	Canny(local, Canny_output, 200, 500, 3);  //边缘检测

	imshow("canny", Canny_output);

	findContours(Canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //轮廓提取
	Mat drawing = Mat::zeros(Canny_output.size(), CV_8UC3); //新建一样大小的图像
	for (int i = 0; i < contours.size();i++)   //每个新的边缘赋RBG随机数
	{
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); //画出轮廓
	}
	imshow("contours", drawing);*/


//	Mat result;
//	Canny(image, result, 50, 150, 3);
//	//cv::imshow("localThreshold", local);
	//imshow("jieguo", result);


//使用不同的二值化方法,再进行判断

	// 全局二值化
	int th = 100;
	cv::Mat global;//全局二值化图像global
	cv::threshold(imageGray, global, th, 255,CV_THRESH_BINARY);


	// 局部自适应阈值二值化

	int blockSize =95;//邻域范围(2-40:95,-35)(50:95,-20)(60:95,-5)(70-80:385,-5)
	int constValue =-5;//补偿值
	cv::Mat local; //局部二值化图像local
	cv::adaptiveThreshold(imageGray, local, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, blockSize, constValue);

    imshow("global", global);
    imshow("local", local);




	int rows = local.rows; //获取图像像素行
	int cols = local.cols;    //获取图像像素列
    vector<vector<Point>> contours;  // 轮廓容器
	//vector<vector<Point>> contours1;
	Rect centerRect(rows / 2 - 10, cols / 2 - 10, 20, 20); //定义中心矩阵

     vector<Vec4i> hierarcy;   
     findContours(local, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
     vector<Rect> boundRect(contours.size());  //定义外接矩形集合
     vector<RotatedRect> box(contours.size()); //定义最小外接矩形集合
	// vector<Point> cen;  //中心点集合
     Point2f rect[4];
	 //Point cen
    for(int i=0; i<contours.size(); i++)    {
        box[i] = minAreaRect(Mat(contours[i]));  //计算每个轮廓最小外接矩形2         boundRect[i] = boundingRect(Mat(contours[i]));
		double area = (box[i].size.width)* (box[i].size.height);//计算面积
		if (area > 1)
		{
			//cout << "矩形面积为: " << area << endl;
			//判断外接矩形的中心点是否为图片的中心
			if (box[i].center.x > centerRect.x && box[i].center.y > centerRect.y &&  box[i].center.y < centerRect.y + centerRect.height &&box[i].center.x < centerRect.x + centerRect.width)

			{

				circle(image, Point(box[i].center.x, box[i].center.y), 2, Scalar(0, 255, 0), -1, 8);  //绘制最小外接矩形的中心点
				cout << "矩形面积为: " << area << endl;
				box[i].points(rect);  //把最小外接矩形四个端点复制给rect数组
				//double area = (box[i].size.width)* (box[i].size.height);//计算面积
				//cout << "矩形面积为: " << area << endl;
				rectangle(image, Point(boundRect[i].x, boundRect[i].y), Point(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height), Scalar(0, 255, 0), 2, 8);
				//double recArea = contourArea(box[i],false);
				for (int j = 0; j < 4; j++)
				{
					line(image, rect[j], rect[(j + 1) % 4], Scalar(0, 0, 255), 2, 8);  //绘制最小外接矩形每条边
				}
			}
		}
    }    
    imshow("dst", image);





/*	if (local.at<uchar>((local.rows) / 2, (local.cols) / 2)  = 0 && global.at<uchar>((local.rows) / 2, (local.cols) / 2)  > 0)

		//cv::imwrite("global.jpg", global);
		//cv::imwrite("local.jpg", local)
	{
	
		int global_whiteNum = 0;
		int global_blackNum = 0;
		int rows = global.rows;
		int cols = global.cols;
		for (int j = 0; j < rows; j++)//遍历二值图像
		{
			for (int i = 0; i < cols; i++)  
			{
				if (global.at<uchar>(j, i) > 0) //计算黑白像素点总个数
				{
					global_whiteNum++;
				}
				else global_blackNum++;
			}
		}
		imshow("image", image);
		imshow("Gray", imageGray);
		cv::imshow("globalThreshold", global);
		//cv::imshow("localThreshold", local);
		cout << "global白色像素点一共有:" << global_whiteNum << endl;
		cout << "global黑色像素点一共有:" << global_blackNum << endl;
		
		
		
		
	}

	else {
		
		int local_blackNum = 0;
		int local_whiteNum = 0;
		int rows = local.rows;
		int cols = local.cols;
		for (int j = 0; j < rows; j++)
		{
			for (int i = 0; i < cols; i++)
			{
				if (local.at<uchar>(j, i) > 0)
				{
					local_whiteNum++;
				}
				else local_blackNum++;
			}
		}

		imshow("image", image);
		imshow("Gray", imageGray);
		//cv::imshow("globalThreshold", global);
		cv::imshow("localThreshold", local);
		cout << "local白色像素点一共有:" << local_whiteNum << endl;
		cout << "local黑色像素点一共有:" << local_blackNum << endl;
	}

	*/
	cv::waitKey(0);


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import matplotlib.pyplot as plt import numpy as np import pandas as pd data = pd.read_excel(r"G:\Data.xlsx") def dis(WC, data): WCX = (np.array(data['x']) * WC).sum() WCY = (np.array(data['y']) * WC).sum() x0 = WCX / WC.sum() y0 = WCY / WC.sum() d_j = ((np.array(data['x']) - x0) ** 2 + (np.array(data['y']) - y0) ** 2) ** 0.5 T = (WC * d_j).sum() print('重心法初始选点大致位置:({},{})'.format(x0, y0)) print('总费用T0:{}'.format(T)) # 迭代10次 plt.rcParams['font.sans-serif'] = [u'SimHei'] for i in range(10): WC_j = WC / d_j WCX_j = ((np.array(data['x']) * WC) / d_j).sum() WCY_j = ((np.array(data['y']) * WC) / d_j).sum() x = WCX_j / WC_j.sum() y = WCY_j / WC_j.sum() d_j = ((np.array(data['x']) - x) ** 2 + (np.array(data['y']) - y) ** 2) ** 0.5 T = (WC * d_j).sum() print('经{}次迭代后选址点位置:({},{})'.format(i + 1, x, y)) print('总费用T{}:{}'.format(i + 1, T)) # 画图,如果需要迭代次数多,建议只画第一次和最后一次 plt.figure(figsize=(8, 4)) plt.scatter(np.array(data['x']), np.array(data['y']), [300, 300, 300, 300, 300], c='green', marker='*', alpha=0.7, label='站点') plt.scatter(x, y, [270], c='red', marker='p', alpha=0.7, label='选址点') plt.xlabel('x坐标', fontsize=11) plt.ylabel('y坐标', fontsize=11) plt.grid(True) plt.title('重心法选址,第{}次结果示意图'.format(i + 1), fontsize=14) plt.show() if __name__ == '__main__': # 读取文件 data = pd.read_excel(r"C:\Users\pan15\Desktop\重心法\data.xlsx") # print(data) WC = np.array(data['W']) * np.array(data['C']) dis(WC, data)
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值