def main(): import sys try: fn = sys.argv[1] except: fn = 'example_wood.jpg' img = cv.imread(cv.samples.findFile(fn)) if img is None: print('Failed to load image file:', fn) sys.exit(1) img_original = img.copy() cv.imshow('original', img_original) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda: ((255, 255, 255), 255)) while True: ch = cv.waitKey() if ch == 27: break if ch == ord(' '): res = cv.inpaint(img_mark, mark, 3, cv.INPAINT_TELEA) cv.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() print('Done')
def __init__(self, fn): self.img = cv2.imread(fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)
def __init__(self, fn): self.img = cv.imread(fn) if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)
def __init__(self, fn): #self.img = cv.imread(fn) self.img = fn if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32(list(np.ndindex(2, 2, 2))) * 255 self.thresh_value = 50 self.thresh_max = None self.auto_update = True self.preprocess(0) self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) self.boundary_points = []
def main(): canvas = numpy.ones((h + tah, w)) cv2.line(canvas, (0, tah - 1), (w, tah - 1), (0, 0, 0), 1) cv2.line(canvas, (145, 0), (145, tah - 1), (.5, 255, 255), 1) font = cv2.FONT_HERSHEY_PLAIN font2 = cv2.FONT_HERSHEY_TRIPLEX text1 = "Press 'C' to clear" cv2.putText(canvas, text1, (5, 10), font, 0.9, (0.5, 255, 0), 1) text2 = "Press 'T' to test" cv2.putText(canvas, text2, (5, 22), font, 0.9, (0.5, 255, 0), 1) img = canvas.copy() sketch = Sketcher('Draw', [img], lambda: ((0, 255, 0), 255)) while (True): if (cv2.waitKey() == ord('c')): print("Clear") img[:] = canvas sketch.show() if (cv2.waitKey() == ord('t')): process(img) output = str(random.randint(0, 10)) cv2.putText(img, output, (162, 22), font2, 1, (0, 255, 0), 1) sketch.show() print("Test") if (cv2.getWindowProperty('Draw', 0) == -1) or (cv2.waitKey() == 27): break cv2.destroyAllWindows()
import numpy as np import cv2 from common import Sketcher if __name__ == '__main__': import sys try: fn = sys.argv[1] except: fn = '../cpp/fruits.jpg' print __doc__ img = cv2.imread(fn) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda: ((255, 255, 255), 255)) while True: ch = 0xFF & cv2.waitKey() if ch == 27: break if ch == ord(' '): res = cv2.inpaint(img_mark, mark, 3, cv2.INPAINT_TELEA) cv2.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() cv2.destroyAllWindows()
def main(): """ Main method of the program. """ # Pull system arguments try: fn = sys.argv[1] except: fn = INPUT_IMAGE # Load the image and store into a variable #image = cv2.imread(cv2.samples.findFile(fn)) image = cv2.imread('mango_img_752.png') if image is None: print('Failed to load image file:', fn) sys.exit(1) # Create an image for sketching the mask image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) # Sketch a mask while True: ch = cv2.waitKey() if ch == 27: # ESC - exit break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # define range of white color in HSV lower_white = np.array([0, 0, 255]) upper_white = np.array([255, 255, 255]) # Create the mask mask = cv2.inRange(image_mark, lower_white, upper_white) # Create the inverted mask mask_inv = cv2.bitwise_not(mask) # Convert to grayscale image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Extract the dimensions of the original image rows, cols, channels = image.shape #print(image.shape) image = image[0:rows, 0:cols] # Bitwise-OR mask and original image colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] cv2.imshow('Mask', colored_portion) # Bitwise-OR inverse mask and grayscale image gray_portion = cv2.bitwise_or(gray, gray, mask=mask_inv) gray_portion = np.stack((gray_portion, ) * 3, axis=-1) # Combine the two images output = colored_portion + gray_portion # Save the image #cv2.imwrite(OUTPUT_IMAGE, output) # Create a table showing input image, mask, and output mask = np.stack((mask, ) * 3, axis=-1) table_of_images = np.concatenate((image, mask, output), axis=1) #cv2.imwrite(TABLE_IMAGE, table_of_images) # Display images, used for debugging '''cv2.imshow('Original Image', image) cv2.imshow('Sketched Mask', image_mark) cv2.imshow('Mask', mask) cv2.imshow('Output Image', output)''' #cv2.imshow('Table of Images', table_of_images) cv2.waitKey(0) # Wait for a keyboard event
def main(): try: fn = sys.argv[1] except: fn = INPUT_IMAGE image = cv2.imread(cv2.samples.findFile(fn)) # Resim bulunamazsa program bozulmasın if image is None: print('Failed to load image file:', fn) sys.exit(1) # Common dosyamızdan örnek oluşturalım image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) # Kullanıcı Komut alanı while True: ch = cv2.waitKey() if ch == 27: # ESC - exit break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # Ten rengine göre uygun renk aralığı ataması lower_white = np.array([50, 50, 50]) upper_white = np.array([255, 255, 255]) mask = cv2.inRange(image_mark, lower_white, upper_white) # Maskeleme işlemi mask_inv = cv2.bitwise_not(mask) # Renk dönüşüm alanı gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rows, cols, channels = image.shape image = image[0:rows, 0:cols] colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] gray_portion = cv2.bitwise_or(gray, gray, mask=mask_inv) gray_portion = np.stack((gray_portion, ) * 3, axis=-1) # Çıktı oluşturulan alan output = colored_portion + gray_portion # Çıktıyı kayıt edelim cv2.imwrite(OUTPUT_IMAGE, output) # Fotoğrafın ilk ve son halini tabloya dönüştürelim mask = np.stack((mask, ) * 3, axis=-1) table_of_images = np.concatenate((image, mask), axis=1) cv2.imwrite(TABLE_IMAGE, table_of_images) # Kullanıcıya gösterelim #cv2.imshow('Original Image', image) #cv2.imshow('Output Image', output) cv2.imshow('Table of Images', table_of_images) cv2.waitKey(0)
def main(): """ Main method of the program. """ imgList = [] for img in all_files: imgList.append(img) print(len(imgList)) INPUT_IMAGE = imgList[14] IMAGE_NAME = INPUT_IMAGE[:INPUT_IMAGE.index(".")] OUTPUT_IMAGE = IMAGE_NAME + "_output.png" try: fn = sys.argv[1] except: fn = INPUT_IMAGE image = cv2.imread(INPUT_IMAGE) #cv2.imshow('Mask', image) #print(img) if image is None: print('Failed to load image file:', fn) sys.exit(1) # Create an image for sketching the mask image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) while True: ch = cv2.waitKey() if ch == 27: # ESC - exit cv2.destroyAllWindows() break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # define range of white color in HSV lower_white = np.array([0, 0, 255]) upper_white = np.array([255, 255, 255]) # Create the mask mask = cv2.inRange(image_mark, lower_white, upper_white) # Create the inverted mask mask_inv = cv2.bitwise_not(mask) # Convert to grayscale image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Extract the dimensions of the original image rows, cols, channels = image.shape #print(image.shape) image = image[0:rows, 0:cols] # Bitwise-OR mask and original image colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] os.chdir( "/Users/pavankumar/Documents/Processed Datasets/scripts/msk_unripen/") #out = os.path.join(os.getcwd() , '/MaskedImages/').join(OUTPUT_IMAGE) print(OUTPUT_IMAGE) cv2.imwrite(OUTPUT_IMAGE, colored_portion) cv2.waitKey(0) # Wait for a keyboard event
def draw(h, w, tah): canvas = np.ones((h + tah, w), dtype=np.float32) cv2.line(canvas, (0, tah - 1), (w, tah - 1), (0, 0, 0), 1) cv2.line(canvas, (201, 0), (201, h + tah), (.5, 255, 255), 1) cv2.line(canvas, (402, 0), (402, h + tah), (.5, 255, 255), 1) font = cv2.FONT_HERSHEY_PLAIN font2 = cv2.FONT_HERSHEY_TRIPLEX font3 = cv2.FONT_HERSHEY_SIMPLEX font4 = cv2.FONT_HERSHEY_DUPLEX #left box text1 = "Press 'C' to clear" cv2.putText(canvas, text1, (20, 10), font, 0.9, (0.5, 255, 0), 1) text2 = "Press 'E' to evaluate" cv2.putText(canvas, text2, (20, 22), font, 0.9, (0.5, 255, 0), 1) #middle box text3 = "Prediction" cv2.putText(canvas, text3, (240, 22), font3, .7, (0, 255, 0), 1) #right box text4 = "Confidence" cv2.putText(canvas, text4, (440, 22), font3, .7, (0, 255, 0), 1) img = canvas.copy() sketch = Sketcher(name, [img], lambda: ((0, 255, 0), 255)) while (True): if cv2.waitKey(0) == ord('c'): # clear the screen print("Clear") img[:] = canvas sketch.show() if cv2.waitKey(0) == ord('e'): # evaluate drawing # remove previous results from screen cv2.rectangle(img, (202, tah), (400, h + tah), (255, 255, 255), -1) cv2.rectangle(img, (404, tah), (600, h + tah), (255, 255, 255), -1) img2 = process( img) #call process img to apply filtering and edge detection #convert image into tensor input to_eval = tf.estimator.inputs.numpy_input_fn(x={"x": img2}, y=None, shuffle=False) # evaluate input image, Returns np.array output1 = classifier.predict(input_fn=to_eval) output2 = classifier.predict(input_fn=to_eval) #extract prediction class and probabilities from classfier output_classes = [c['classes'] for c in output1] output_prob = [p['probabilities'] for p in output2] #sort probabilities probs = output_prob[0] vals = [] for i in range(0, len(probs)): vals.append((i, probs[i])) sorted_probs = sorted(vals, key=lambda x: x[1]) #determine confidency level if sorted_probs[-1][1] >= 0.9: conf = 'Very Confident' elif sorted_probs[-1][1] < 0.9 and sorted_probs[-1][1] >= 0.8: conf = 'Confident' elif sorted_probs[-1][1] < 0.8 and sorted_probs[-1][1] >= 0.65: conf = 'Somewhat Confident' elif sorted_probs[-1][1] < 0.65: conf = 'Not Confident' # print evaluation confidence prob2 = str(sorted_probs[-2][0]) + ' = ' + str(sorted_probs[-2][1]) cv2.putText(img, conf, (430, 115), font3, .6, (0, 255, 0), 1) # print prediction output = str(output_classes) cv2.putText(img, str(output[1]), (250, 150), font4, 4, (0, 255, 0), 3) sketch.show() print("Eval") if (cv2.getWindowProperty(name, 0) == -1) or (cv2.waitKey() == 27): break cv2.destroyAllWindows()