Missing triangle problem with Delaunay triangulation, opencv2. /u/Beryllium5032 Python Education

I have to find a way to triangulate the seas of the moon, in order to calculate their aera.

I first turned the original image into a blurred version of itself, then made evry pixel either black, or either white, based on a threshhold contract parameter. (contrast_coef) Then I used the Canny edge detection, to detect the edges of the moon/the seas, and randomly selected a certain amount of points on these edges. That’s how I got my points.

What I aimed to do, was to then use Delaunay triangulation on this set of points, and only keeping the triangles which center is a black pixel on the contrasted image.

This method obviouly isn’t perfect, but there are a lot of triangles that just mysteriously dissapear, even though they should be picked by the method I used. Some triangles should, after verifications, obviously be picked, bacause they literally are fully inside the black aera on the contrasted image, yet a lot are missing.

I do not know if it is a glitch, an erreor of mine, or if some triangles are just missing in the triangulation. As if three other triangles had points in common with the missing one, and that the edges of the missing one are not grouped, but rather one edge is grouped with the other triangle next to it or something.

https://imgur.com/a/W6nI6xd

in theory, if no triangles where missing, all triangles should either be red (rejected) or green (accpted). But some have different-colored edges, which make me believe the delaunay triangulation misses certain triangles which edges are already used for other triangles.

“` from math import* import numpy as np from matplotlib import* from PIL import Image, ImageDraw import cv2 as cv2 from random import * from copy import *

“(palier_contraste is the contrast threshhold)” “Contrasted threshhold image” def IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste): src = cv2.imread(Image) src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) gray_blurred = cv2.blur(src_gray, (blur1,blur2)) ret,imm = cv2.threshold(gray_blurred,palier_contraste,255,cv2.THRESH_BINARY) cv2.imshow(‘image contraste’,imm) return imm

dp=3 mindist=10000 blur3=10 blur4=10

“Circle detection” def CIRCLE(Image): L=[] src = cv2.imread(Image) gray_blurred = IMAGE_CONTRASTE(Image, 10, 10, 10) (lign,colo)=(len(src[0]),len(src)) rmax=int(np.sqrt(lign2+colo2)) circles=cv2.HoughCircles(gray_blurred,cv2.HOUGH_GRADIENT,dp,mindist,param1=100,param2=70,minRadius=0,maxRadius=rmax) circles = np.round(circles[0, :]).astype(“int”) for(x,y,r) in circles: cv2.circle(src, (x,y), r, (0,255,0), 3) L.append((x,y,r)) cv2.imshow(‘image avec cercle’,src) return L,src

“Rectangle where the moon is. Unimportant” def RECT(Image): L=CIRCLE(Image)[0][0] (x,y)=(L[0]-L[2],L[1]-L[2]) return (x,y,2L[2],2L[2])

k1=100 k2=100

“Edge detection” def BORDS(Image, k1, k2, blur1, blur2, palier_contraste): image=IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste) im=cv2.Canny(image,k1,k2) L=[] M=[] R=CIRCLE(Image)[0] (colo,lign)=(len(image[0]),len(image)) for x in range(colo): for y in range(lign): if im[y][x]==255: L.append([x,y]) for (x,y) in L: im[y][x]=255 cv2.imshow(‘a’,im) return L

“n : number of points”

def POINTS(Image, k2, k1, blur1, blur2, palier_contraste, n): image=cv2.imread(Image) L=BORDS(Image, k1, k2, blur1, blur2, palier_contraste) M=sample(L,n) for (x,y) in M: cv2.circle(image, (x,y), 1, (0, 255, 0), 6) cv2.imshow(”,image) return M

def TRIANGULATION(Image, k1, k2, blur1, blur2, palier_contraste, n): rect=RECT(Image) image=cv2.imread(Image) points=POINTS(Image, k1, k2, blur1, blur2, palier_contraste, n) subdiv =cv2.Subdiv2D(rect) subdiv.insert(points) return subdiv.getTriangleList()

“sea triangles only” def TRIANGLE_MERS(Image,k1,k2,blur1,blur2,palier_contraste,n): L=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) M=[] I=IMAGE_CONTRASTE(Image,2blur1,2blur2,palier_contraste-10) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M

“Only for the visualisation. Unimportant” def TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,L): M=[] I=IMAGE_CONTRASTE(Image,blur1,blur2,palier_contraste) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M

def VISUALIZE_TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n): M=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) L=TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,M) src=cv2.imread(Image) for j in range(len(M)): points=np.array([[M[j][0],M[j][1]],[M[j][2],M[j][3]],[M[j][4],M[j][5]]],np.int32) cv2.polylines(src,[points],True,(0,0,255),1) for i in range(len(L)): points=np.array([[L[i][0],L[i][1]],[L[i][2],L[i][3]],[L[i][4],L[i][5]]],np.int32) cv2.polylines(src,[points],True,(0,255,0),1) cv2.imshow(‘a’,src) “`

Is there a way not to have missing triangles?

submitted by /u/Beryllium5032
[link] [comments]

​r/learnpython I have to find a way to triangulate the seas of the moon, in order to calculate their aera. I first turned the original image into a blurred version of itself, then made evry pixel either black, or either white, based on a threshhold contract parameter. (contrast_coef) Then I used the Canny edge detection, to detect the edges of the moon/the seas, and randomly selected a certain amount of points on these edges. That’s how I got my points. What I aimed to do, was to then use Delaunay triangulation on this set of points, and only keeping the triangles which center is a black pixel on the contrasted image. This method obviouly isn’t perfect, but there are a lot of triangles that just mysteriously dissapear, even though they should be picked by the method I used. Some triangles should, after verifications, obviously be picked, bacause they literally are fully inside the black aera on the contrasted image, yet a lot are missing. I do not know if it is a glitch, an erreor of mine, or if some triangles are just missing in the triangulation. As if three other triangles had points in common with the missing one, and that the edges of the missing one are not grouped, but rather one edge is grouped with the other triangle next to it or something. https://imgur.com/a/W6nI6xd in theory, if no triangles where missing, all triangles should either be red (rejected) or green (accpted). But some have different-colored edges, which make me believe the delaunay triangulation misses certain triangles which edges are already used for other triangles. “` from math import* import numpy as np from matplotlib import* from PIL import Image, ImageDraw import cv2 as cv2 from random import * from copy import * “(palier_contraste is the contrast threshhold)” “Contrasted threshhold image” def IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste): src = cv2.imread(Image) src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) gray_blurred = cv2.blur(src_gray, (blur1,blur2)) ret,imm = cv2.threshold(gray_blurred,palier_contraste,255,cv2.THRESH_BINARY) cv2.imshow(‘image contraste’,imm) return imm dp=3 mindist=10000 blur3=10 blur4=10 “Circle detection” def CIRCLE(Image): L=[] src = cv2.imread(Image) gray_blurred = IMAGE_CONTRASTE(Image, 10, 10, 10) (lign,colo)=(len(src[0]),len(src)) rmax=int(np.sqrt(lign2+colo2)) circles=cv2.HoughCircles(gray_blurred,cv2.HOUGH_GRADIENT,dp,mindist,param1=100,param2=70,minRadius=0,maxRadius=rmax) circles = np.round(circles[0, :]).astype(“int”) for(x,y,r) in circles: cv2.circle(src, (x,y), r, (0,255,0), 3) L.append((x,y,r)) cv2.imshow(‘image avec cercle’,src) return L,src “Rectangle where the moon is. Unimportant” def RECT(Image): L=CIRCLE(Image)[0][0] (x,y)=(L[0]-L[2],L[1]-L[2]) return (x,y,2L[2],2L[2]) k1=100 k2=100 “Edge detection” def BORDS(Image, k1, k2, blur1, blur2, palier_contraste): image=IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste) im=cv2.Canny(image,k1,k2) L=[] M=[] R=CIRCLE(Image)[0] (colo,lign)=(len(image[0]),len(image)) for x in range(colo): for y in range(lign): if im[y][x]==255: L.append([x,y]) for (x,y) in L: im[y][x]=255 cv2.imshow(‘a’,im) return L “n : number of points” def POINTS(Image, k2, k1, blur1, blur2, palier_contraste, n): image=cv2.imread(Image) L=BORDS(Image, k1, k2, blur1, blur2, palier_contraste) M=sample(L,n) for (x,y) in M: cv2.circle(image, (x,y), 1, (0, 255, 0), 6) cv2.imshow(”,image) return M def TRIANGULATION(Image, k1, k2, blur1, blur2, palier_contraste, n): rect=RECT(Image) image=cv2.imread(Image) points=POINTS(Image, k1, k2, blur1, blur2, palier_contraste, n) subdiv =cv2.Subdiv2D(rect) subdiv.insert(points) return subdiv.getTriangleList() “sea triangles only” def TRIANGLE_MERS(Image,k1,k2,blur1,blur2,palier_contraste,n): L=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) M=[] I=IMAGE_CONTRASTE(Image,2blur1,2blur2,palier_contraste-10) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M “Only for the visualisation. Unimportant” def TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,L): M=[] I=IMAGE_CONTRASTE(Image,blur1,blur2,palier_contraste) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M def VISUALIZE_TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n): M=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) L=TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,M) src=cv2.imread(Image) for j in range(len(M)): points=np.array([[M[j][0],M[j][1]],[M[j][2],M[j][3]],[M[j][4],M[j][5]]],np.int32) cv2.polylines(src,[points],True,(0,0,255),1) for i in range(len(L)): points=np.array([[L[i][0],L[i][1]],[L[i][2],L[i][3]],[L[i][4],L[i][5]]],np.int32) cv2.polylines(src,[points],True,(0,255,0),1) cv2.imshow(‘a’,src) “` Is there a way not to have missing triangles? submitted by /u/Beryllium5032 [link] [comments] 

I have to find a way to triangulate the seas of the moon, in order to calculate their aera.

I first turned the original image into a blurred version of itself, then made evry pixel either black, or either white, based on a threshhold contract parameter. (contrast_coef) Then I used the Canny edge detection, to detect the edges of the moon/the seas, and randomly selected a certain amount of points on these edges. That’s how I got my points.

What I aimed to do, was to then use Delaunay triangulation on this set of points, and only keeping the triangles which center is a black pixel on the contrasted image.

This method obviouly isn’t perfect, but there are a lot of triangles that just mysteriously dissapear, even though they should be picked by the method I used. Some triangles should, after verifications, obviously be picked, bacause they literally are fully inside the black aera on the contrasted image, yet a lot are missing.

I do not know if it is a glitch, an erreor of mine, or if some triangles are just missing in the triangulation. As if three other triangles had points in common with the missing one, and that the edges of the missing one are not grouped, but rather one edge is grouped with the other triangle next to it or something.

https://imgur.com/a/W6nI6xd

in theory, if no triangles where missing, all triangles should either be red (rejected) or green (accpted). But some have different-colored edges, which make me believe the delaunay triangulation misses certain triangles which edges are already used for other triangles.

“` from math import* import numpy as np from matplotlib import* from PIL import Image, ImageDraw import cv2 as cv2 from random import * from copy import *

“(palier_contraste is the contrast threshhold)” “Contrasted threshhold image” def IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste): src = cv2.imread(Image) src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) gray_blurred = cv2.blur(src_gray, (blur1,blur2)) ret,imm = cv2.threshold(gray_blurred,palier_contraste,255,cv2.THRESH_BINARY) cv2.imshow(‘image contraste’,imm) return imm

dp=3 mindist=10000 blur3=10 blur4=10

“Circle detection” def CIRCLE(Image): L=[] src = cv2.imread(Image) gray_blurred = IMAGE_CONTRASTE(Image, 10, 10, 10) (lign,colo)=(len(src[0]),len(src)) rmax=int(np.sqrt(lign2+colo2)) circles=cv2.HoughCircles(gray_blurred,cv2.HOUGH_GRADIENT,dp,mindist,param1=100,param2=70,minRadius=0,maxRadius=rmax) circles = np.round(circles[0, :]).astype(“int”) for(x,y,r) in circles: cv2.circle(src, (x,y), r, (0,255,0), 3) L.append((x,y,r)) cv2.imshow(‘image avec cercle’,src) return L,src

“Rectangle where the moon is. Unimportant” def RECT(Image): L=CIRCLE(Image)[0][0] (x,y)=(L[0]-L[2],L[1]-L[2]) return (x,y,2L[2],2L[2])

k1=100 k2=100

“Edge detection” def BORDS(Image, k1, k2, blur1, blur2, palier_contraste): image=IMAGE_CONTRASTE(Image, blur1, blur2, palier_contraste) im=cv2.Canny(image,k1,k2) L=[] M=[] R=CIRCLE(Image)[0] (colo,lign)=(len(image[0]),len(image)) for x in range(colo): for y in range(lign): if im[y][x]==255: L.append([x,y]) for (x,y) in L: im[y][x]=255 cv2.imshow(‘a’,im) return L

“n : number of points”

def POINTS(Image, k2, k1, blur1, blur2, palier_contraste, n): image=cv2.imread(Image) L=BORDS(Image, k1, k2, blur1, blur2, palier_contraste) M=sample(L,n) for (x,y) in M: cv2.circle(image, (x,y), 1, (0, 255, 0), 6) cv2.imshow(”,image) return M

def TRIANGULATION(Image, k1, k2, blur1, blur2, palier_contraste, n): rect=RECT(Image) image=cv2.imread(Image) points=POINTS(Image, k1, k2, blur1, blur2, palier_contraste, n) subdiv =cv2.Subdiv2D(rect) subdiv.insert(points) return subdiv.getTriangleList()

“sea triangles only” def TRIANGLE_MERS(Image,k1,k2,blur1,blur2,palier_contraste,n): L=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) M=[] I=IMAGE_CONTRASTE(Image,2blur1,2blur2,palier_contraste-10) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M

“Only for the visualisation. Unimportant” def TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,L): M=[] I=IMAGE_CONTRASTE(Image,blur1,blur2,palier_contraste) for i in range(n): Y=int((L[i][0]+L[i][2]+L[i][4])/3) X=int((L[i][1]+L[i][3]+L[i][5])/3) if I[X][Y]==0: M.append(L[i]) return M

def VISUALIZE_TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n): M=TRIANGULATION(Image,k1,k2,blur1,blur2,palier_contraste,n) L=TRIANGLE_MERS2(Image,k1,k2,blur1,blur2,palier_contraste,n,M) src=cv2.imread(Image) for j in range(len(M)): points=np.array([[M[j][0],M[j][1]],[M[j][2],M[j][3]],[M[j][4],M[j][5]]],np.int32) cv2.polylines(src,[points],True,(0,0,255),1) for i in range(len(L)): points=np.array([[L[i][0],L[i][1]],[L[i][2],L[i][3]],[L[i][4],L[i][5]]],np.int32) cv2.polylines(src,[points],True,(0,255,0),1) cv2.imshow(‘a’,src) “`

Is there a way not to have missing triangles?

submitted by /u/Beryllium5032
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *