0

I have to analyse a thread of a screw. I have images of the screw without mistakes and images with mistakes. Now I have to write an Program witch distinguish them. screw My problem is that I don't find any conclusions how I can distinguish the images. I tryed to fit an ellipse after I found the contours of the thread and if the ellipse doesent fit the screw ist broken, but it doesn't work. contours

import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
from pathlib import Path
import math
import sys

img_unbearbeitet = cv2.imread('Bild2NIO.jpg',)
plt.imshow(img_unbearbeitet)

## Bild in ein Graustudenbild umwandeln

def rgb2gray(img_unbearbeitet):
img_g=np.dot(img_unbearbeitet, [0.299, 0.578, 0.114])
            
return img_g.astype(np.uint8)

img_g = rgb2gray(img_unbearbeitet)

plt.imshow(img_g)

## Gausfilter an Bild  

kernel = np.ones((5,5),np.float32)/25
img_gaus = cv2.filter2D(img_g,-1,kernel)
plt.imshow(img_gaus)
## Histogrammebnung

img_H = cv2.equalizeHist(img_gaus)
plt.imshow(img_H)
(t,imgbin) = cv2.threshold(img_gaus, 0, 
255,cv2.THRESH_BINARY +cv2.THRESH_OTSU)    
crop_img1 =imgbin[150:500, 80:300]
plt.imshow(crop_img1)
## Konturen finden bei Treshold-Bild
cnts, _ = cv2.findContours(crop_img1, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
len(cnts)
out1= cv2.drawContours(cv2.merge( (crop_img1, crop_img1,  
crop_img1)) , cnts, -1, (0,0,255), 1)

## Canny-Operation mit Bild aus Histogrammebnung

img_canny1 = cv2.Canny(out1,250,150)

plt.subplot(121),plt.imshow(out1,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_canny1,cmap = 'gray')
plt.title('Canny-Bild'), plt.xticks([]), plt.yticks([])
plt.show()
mneumann
  • 713
  • 2
  • 9
  • 42
  • Try asking more specific doubts – Muhammed Jaseem Jun 16 '21 at 22:29
  • Would [these answers](https://stackoverflow.com/questions/27035672/cv-extract-differences-between-two-images) help you? – mneumann Jun 17 '21 at 07:19
  • I would recommend starting with a list of typical defects (missing piece of thread, bent thread, incompletely cut thread, ...) and then you can consider what views/angles/illuminations would reveal that defect. this picture isn't bad but canny will be useless. contours are a decent approach. analyze the **curvature** of those contours that represent the edges of threads, i.e. where the gradient points roughly upwards. – Christoph Rackwitz Jun 27 '21 at 21:43

0 Answers0