0

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. I have been able to achieve this using 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. 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;
}
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
mrrussel
  • 21
  • 4
  • post sample images. Did you try opencvs findContours method? – Micka Aug 01 '16 at 07:16
  • Maybe connected components? – Yang Yu Aug 01 '16 at 07:27
  • I've just edited the post to include the sample image I'm working with and the second image of the edges detected at the top of the post. No I did't try the findContours method but I've tried it now and it doesn't seem to help? – mrrussel Aug 01 '16 at 13:33
  • What do you mean by connected components? – mrrussel Aug 01 '16 at 13:37
  • Are your images always going to be so simple? By that I mean are regions of interest always going to be rectangles that are differentiated enough from the background for Canny to pick out the edges perfectly? – Dave Aug 01 '16 at 15:38
  • No they won't be so simple, I'm just using that image as a proof of concept and once it works for that I can assume it will work for any kind of image. My ultimate goal is to convert black and white videos into color by colorizing specific keyframes at certain intervals, each of which will act as a reference. I then hope to track different objects (defined by edge detection from the reference frames) as they move between two consecutive keyframes - keeping the original luminance information from the footage while applying a color mask with different hues and saturation for different objects. – mrrussel Aug 01 '16 at 16:00
  • I'm willing to accept that this may not work for every kind of image or video though - but I'm hoping to get it to work for at least some types of real life footage - e.g. maybe with simple backgrounds and easily defined objects – mrrussel Aug 01 '16 at 16:06
  • You should try out connected components `cv2.connectedComponentsWithStat()`. It would help – Jeru Luke Jan 29 '17 at 14:18

1 Answers1

0

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. Once you can the lines you can probably sort them in (x,y) coordinates and fill the regions with your desired, unique colors. The image which you uploaded shouldn't have any issues with canny i suppose. Let me know how it works.

Rick M.
  • 3,045
  • 1
  • 21
  • 39