OpenCV之圆形切开和矩形切开的完成ITeye - 众发娱乐

OpenCV之圆形切开和矩形切开的完成ITeye

2019-01-10 17:34:41 | 作者: 智宇 | 标签: 切割,圆内,圆外 | 浏览: 822



圆切割

今日持续昨日的对角切割,想到了切割出来一个圆形区域。于是就很简单的完成了一下。思维仍是使用数学公式判断点是否在圆内仍是圆外。



#include

 

 

#include

 

  #include

 

  #include

 

  using namespace cv; using namespace std; int main(int argc,char* argv[]) { Mat image = imread( quot;1.jpg quot;); Mat image1(image.rows, image.cols, image.type(), Scalar(180, 120, 50)); Mat image2(image.rows, image.cols, image.type(), Scalar(180, 120, 50)); Point center(image.cols/2,image.rows/2); int radius = 200; circle(image, center, radius, Scalar(0, 200, 100), 2, 8, 0); for (int x = 0; x image.cols; x++) { for (int y = 0; y image.rows; y++) { int temp = ((x - center.x) * (x - center.x) + (y - center.y) *(y - center.y)); if (temp (radius * radius)) // 圆内 { image1.at

 

  (Point(x, y))[0] = image.at

 

  (Point(x, y))[0]; image1.at

 

  (Point(x, y))[1] = image.at

 

  (Point(x, y))[1]; image1.at

 

  (Point(x, y))[2] = image.at

 

  (Point(x, y))[2]; } else //圆外 { image2.at

 

  (Point(x, y))[0] = image.at

 

  (Point(x, y))[0]; image2.at

 

  (Point(x, y))[1] = image.at

 

  (Point(x, y))[1]; image2.at

 

  (Point(x, y))[2] = image.at

 

  (Point(x, y))[2]; } } } imshow( quot;image1 quot;, image1); imshow( quot;image2 quot;, image2); while (uchar(waitKey() != q)) {} return 0; }

 

留意Point center(i http://www.cppentry.com  编程开发 程序员入门mage.rows/2,image.cols/2)和Point center(image.cols/2,image.rows/2)的差异。后者才是中心点。

矩形切割



#include

 

 

#include

 

  #include

 

  #include

 

  using namespace cv; using namespace std; int main(int argc,char* argv[]) { Mat image = imread( quot;miao.jpg quot;); Mat image1(image.rows, image.cols, image.type(), Scalar(180, 120, 50)); Mat image2(image.rows, image.cols, image.type(), Scalar(180, 120, 50)); Point point1(140,40); Point point2(470, 340); for (int x = 0; x image.cols; x++) { for (int y = 0; y image.rows; y++) { if (point1.x = x amp; amp; x =point2.x amp; amp; point1.y =y amp; amp; y =point2.y) { image1.at

 

  (Point(x, y))[0] = image.at

 

  (Point(x, y))[0]; image1.at

 

  (Point(x, y))[1] = image.at

 

  (Point(x, y))[1]; image1.at

 

  (Point(x, y))[2] = image.at

 

  (Point(x, y))[2]; } else { image2.at

 

  (Point(x, y))[0] = image.at

 

  (Point(x, y))[0]; image2.at

 

  (Point(x, y))[1] = image.at

 

  (Point(x, y))[1]; image2.at

 

  (Point(x, y))[2] = image.at

 

  (Point(x, y))[2]; } } } namedWindow( quot;image1 quot;, 0); namedWindow( quot;image2 quot;, 0); imshow( quot;image quot;, image); imshow( quot;image1 quot;, image1); imshow( quot;image2 quot;, image2); while (uchar(waitKey() != q)) {} return 0; }

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表众发娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章