import cv import cv2 import numpy as np import tippy.segmentations as se import tippy.basic_operations as bo import tippy.display_operations as do user_input = 1 img_name = "roi.bmp" threshold = 20 connectivity = 4 img = cv.LoadImage(img_name, cv.CV_LOAD_IMAGE_GRAYSCALE) seed = (10, 10) out_img = se.simple_region_growing(img, seed, threshold, connectivity) #do.display_single_image(out_img, "Region Growing result") cv.SaveImage('hj.bmp', out_img)
The following method uses one seed point, defined by the user. The region grows by comparing with its neighbourhood. The chosen criteria is in this case a difference between outside pixel's intensity value and the region's mean. The pixel with minimum intensity in the region neighbouhood is chosen to be included. The growing stops as soon as the difference is larger than a threshold. In this implementation, a 4-connectivity has been chosen. The 8-connectivity should be included soon. Due to the method itself, only grayscale images may be processed for now. So color images should be converted first. ''' import cv import tippy.segmentations as se import tippy.basic_operations as bo import tippy.display_operations as do user_input = 0 img_name = "tippy/data/gnu.jpg" threshold = 20 connectivity = 8 img = cv.LoadImage(img_name, cv.CV_LOAD_IMAGE_GRAYSCALE) if user_input: seed = bo.mouse_point(img, mode="S") # waits for user click to get seed else: seed = (70, 106) out_img = se.simple_region_growing(img, seed, threshold, connectivity) do.display_single_image(out_img, "Region Growing result")
def test_region_growing(self): # Image type tests self.assertRaises( TypeError, lambda: se.simple_region_growing(self.img_3c, self.seed)) self.assertRaises( TypeError, lambda: se.simple_region_growing(self.img_16s, self.seed)) self.assertRaises( TypeError, lambda: se.simple_region_growing(self.fake_img, self.seed)) # Threshold tests self.assertRaises( ValueError, lambda: se.simple_region_growing( self.img_1c, self.seed, self.neg_thres)) self.assertRaises( TypeError, lambda: se.simple_region_growing( self.img_1c, self.seed, self.bad_thres)) # Seed tests self.assertRaises( ValueError, lambda: se.simple_region_growing(self.img_1c, self.neg_seed)) self.assertRaises( ValueError, lambda: se.simple_region_growing(self.img_1c, self.big_seed)) self.assertRaises( TypeError, lambda: se.simple_region_growing(self.img_1c, self.bad_seed)) self.assertRaises( TypeError, lambda: se.simple_region_growing(self.img_1c, self.long_seed)) # output test out_img = se.simple_region_growing(self.img_1c, self.seed) self.assertEqual(out_img.depth, cv.IPL_DEPTH_8U) self.assertEqual(out_img.nChannels, 1) self.assertEqual(cv.GetSize(out_img), cv.GetSize(self.img_1c)) # connectivity test self.assertRaises( TypeError, lambda: se.simple_region_growing( self.img_1c, self.seed, self.thres, self.bad_conn)) self.assertRaises( ValueError, lambda: se.simple_region_growing( self.img_1c, self.seed, self.thres, self.other_conn)) # function result tests # 4-conn img_gnu = cv.LoadImage("data/gnu.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) # 1 channel image out_img = se.simple_region_growing(img_gnu, seed=(70, 106), threshold=20) self.assertEqual(cv.CountNonZero(out_img), 584) # 8-conn img_gnu = cv.LoadImage("data/gnu.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) # 1 channel image out_img = se.simple_region_growing(img_gnu, seed=(70, 106), threshold=20, conn=8) self.assertEqual(cv.CountNonZero(out_img), 627) #if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] #unittest.main()