Go to Post The more interesting arguments were around a related subject: whether it was pronounced "Wago tool" or "screwdriver". - Billfred [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-05-2016, 22:38
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 229
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
OpenCV: findContours() messing with images?

Hi,

We used GRIP for Stronghold and was super nice and easy. Now, I want to take it to the next level with OpenCV.

I am currently having a lot of trouble just trying to find a contour in anything! The findContours function seems to be interfering with something which makes the edges image deteriorate.
For example, here is the Canny edge with and without it being the source of the findContour function: http://imgur.com/a/7kqAw
Here is the super simple code I am using:
Code:
import cv2 as cv
import numpy as np
img = cv.imread("29.jpg")#Sample image of Field
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #Convert to gray...just to try

edges = cv.Canny(gray, 200, 300)
#Finds the contours
image, contours, hierarchy = cv.findContours(edges,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
#Draws
cv.drawContours(img, contours, 1, (0, 255,255), 3)
#Shows
cv.imshow("edges!", edges)
cv.imshow("mask!", gray)
cv.imshow("frame!", img)

k = cv.waitKey(0) 
cv.destroyAllWindows()
Because the image deteriorates when put through the findContours function, I feel that it is never able to find anything. This doesn't just happen when I put the edges image through, it happens when I try things like masks too.

I am super new to OpenCV so there is a strong possibility that there is something that I am doing completely wrong.

Any help would greatly be appreciated!
__________________
#Python4Life
Reply With Quote
  #2   Spotlight this post!  
Unread 08-05-2016, 22:43
Jaci's Avatar
Jaci Jaci is offline
Registered User
AKA: Jaci R Brunning
FRC #5333 (Can't C# | OpenRIO)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Perth, Western Australia
Posts: 265
Jaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond repute
Re: OpenCV: findContours() messing with images?

If you're using findContours(), you don't need to use Canny Edge Detection. By using Canny first, you're narrowing down the image and giving findContours() less information to work with, and so it's tolerances will cut out anything too small

Here's our code for acquiring the goal, although it is in C++ instead of Python I believe the API is roughly the same
__________________
Jacinta R

Curtin FRC (5333+5663) : Mentor
5333 : Former [Captain | Programmer | Driver], Now Mentor
OpenRIO : Owner

Website | Twitter | Github
jaci.brunning@gmail.com
Reply With Quote
  #3   Spotlight this post!  
Unread 08-05-2016, 23:01
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 229
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: OpenCV: findContours() messing with images?

Quote:
Originally Posted by Jaci View Post
If you're using findContours(), you don't need to use Canny Edge Detection. By using Canny first, you're narrowing down the image and giving findContours() less information to work with, and so it's tolerances will cut out anything too small

Here's our code for acquiring the goal, although it is in C++ instead of Python I believe the API is roughly the same
Alright, makes sense. But do you know why it makes the input image so weird even though it isn't outputing to it? That seems to be the problem and it affects everything from edge input to masks.
__________________
#Python4Life
Reply With Quote
  #4   Spotlight this post!  
Unread 09-05-2016, 00:11
Cabey4 Cabey4 is offline
Vision/Scouting/Strategy/Too much
AKA: Tom Schwarz
no team
Team Role: Programmer
 
Join Date: May 2015
Rookie Year: 2015
Location: Sydney
Posts: 26
Cabey4 is on a distinguished road
Re: OpenCV: findContours() messing with images?

Firstly, I would recommend converting to HSV and doing HSV filtering at some point (but it sounds like gray was just temporary).

I think doing HSV filtering would help a bit, but it also might not.
I can PM you our code, it will be going to github once I've cleaned it up but I can give it you now if it would help.

Also, in my code if I draw contours I don't just call the function, I put the result into a varialbe, like:
Code:
outImage = cv2.drawContours(origImage, contours, -1, (255, 255, 0), 1)
and it looks like you don't have that.

So I would say to do HSV filtering now, I can give you my code to do it if you want, and to also look into the variable return thingy.
__________________
Reply With Quote
  #5   Spotlight this post!  
Unread 09-05-2016, 02:21
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: OpenCV: findContours() messing with images?

Quote:
Originally Posted by OpenCV Docs
If you want to keep the unmodified image, create a copy before calling findContours:

Code:
import cv2 as cv
import numpy as np
img = cv.imread("29.jpg")#Sample image of Field
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #Convert to gray...just to try

edges = cv.Canny(gray, 200, 300)
#Finds the contours
scratch = edges.copy()
image, contours, hierarchy = cv.findContours(scratch,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
#Draws
cv.drawContours(img, contours, 1, (0, 255,255), 3)
#Shows
cv.imshow("edges!", edges)
cv.imshow("mask!", gray)
cv.imshow("frame!", img)

k = cv.waitKey(0) 
cv.destroyAllWindows()
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 23:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi