简体   繁体   English

使用带有opencv库的边缘检测程序将图像分离为组件进行处理

[英]Separating an image into components using an edge detection program with opencv libraries for processing

image I want to detect edges from: click here edges drawn by finding canny edges in program: click here I am using the opencv library with processing so that I can perform edge detection in order to split my image into different objects / components. 我想从以下位置检测边缘的图像:单击此处 ,通过在程序中查找Canny边缘绘制的边缘:单击此处,我正在使用opencv库进行处理,以便执行边缘检测,以便将图像拆分为不同的对象/组件。 I have been able to achieve this using canny edge detection. 我已经能够使用Canny Edge Detection实现此目的。 This gives me a second image of a black background with grey lines representing where edges have been detected. 这给了我黑色背景的第二张图像,上面有灰线代表检测到边缘的位置。 However, I then need to be able to separate this black image with edges into recognizable regions / objects that the program will understand as being different - I was thinking if I could assign a unique color for each region bounded by an edge. 但是,然后我需要能够将带有边缘的黑色图像分离为程序可以理解为不同的可识别区域/对象-我在考虑是否可以为每个以边缘为边界的区域分配唯一的颜色。 Then I would be able to loop through the whole image's pixel array and treat each region differently depending on its unique color. 然后,我将能够遍历整个图像的像素阵列,并根据其独特的颜色对每个区域进行不同的处理。

However, my problem is that I do not know how to get processing to assign a unique color for each region which is bounded by an edge. 但是,我的问题是我不知道如何获得处理以为每个以边为边界的区域分配唯一的颜色。 I have tried using for loops in countless ways so that it can find and assign different regions but no matter what I try it hasn't worked. 我尝试了无数种方式使用for循环,以便它可以查找和分配不同的区域,但是无论我尝试什么,都行不通。 Was wondering if anyone has any solutions? 想知道是否有人有任何解决方案? Here is the code: 这是代码:

import gab.opencv.*;

import processing.video.*;

OpenCV opencv;
Capture src;
PImage canny, ref, comb, comb2, tiles;
color [] combColour = new color [0];
int c = 0;
int threshold = 20;

int a = 100;
int b = 100;
int x = 0;
int y = 0;
Boolean dir = true;
int ydirection = 1;
int xdirection = 1;

void setup(){
  src = new Capture(this, 640, 480);
  size(640, 480, P2D);
  src.start();

  ref = createImage(width/2, height/2, HSB);
  tiles = loadImage("tiles2.png");

  opencv = new OpenCV(this, tiles);
  opencv.findCannyEdges(20, 75);
  canny = opencv.getSnapshot();

}

void draw(){
updatePixels();
src.read();



 loadPixels();
 pushMatrix();

image(tiles, 0, 0, width/2, height/2);


opencv.loadImage(tiles);
opencv.findCannyEdges(20, 75);
canny = opencv.getSnapshot();




image(canny, 0, height/2, width/2, height/2);
if (c == 5){
comb = get(0, height/2, width/2, height/2);
comb2 = get(0, height/2, width/2, height/2);
}
if ( c >= 5){
comb.loadPixels();
int loc = x + y*comb.width;
color currentColor = comb.pixels[loc];
if (brightness(currentColor) < 10){

 comb.pixels[loc] = color(hue(a), saturation(b), brightness(currentColor));

}

else if (brightness(currentColor) > 150){
 comb.pixels[loc] = currentColor;
   if ( a >= 235){

 a = 0;
}
else{
 a += 20;
}
 if ( b >= 235){
 b = 0;
}
else {
 b += 20;
}

}

if (y  >= comb.height -1){ 
x += 1;
y = 0;
println("one" + x);
}
else{
y += 1;
println("two" + y);
}

println("a " + a);

comb.updatePixels();



image(comb, width/2, height/2);
image(comb2, width/2, 0);
 }

popMatrix();

updatePixels();

c += 1;
}

I am probably late in answering this question but may be you can try using Hough Probabilistic Transform, http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html . 我可能回答这个问题的时间很晚,但是也许您可以尝试使用霍夫概率变换( http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html) Once you can the lines you can probably sort them in (x,y) coordinates and fill the regions with your desired, unique colors. 一旦可以绘制线条,就可以按(x,y)坐标对它们进行排序,并用所需的独特颜色填充区域。 The image which you uploaded shouldn't have any issues with canny i suppose. 我想你上传的图像应该不会有问题。 Let me know how it works. 让我知道它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM