├── demo1.py ├── demo2.py ├── demo3.py └── README.md /demo1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | ## Written by Andrew Cutts 4 | 5 | import cv2 6 | 7 | def click_event(event, x, y, flags, param): 8 | if event == cv2.EVENT_LBUTTONDOWN: 9 | print x, y 10 | if event == cv2.EVENT_RBUTTONDOWN: 11 | red = img[y,x,2] 12 | blue = img[y,x,0] 13 | green = img[y,x,1] 14 | print red, green, blue 15 | strRGB = str(red) + "," + str(green) + "," +str(blue) 16 | font = cv2.FONT_HERSHEY_SIMPLEX 17 | cv2.putText(img,strRGB,(x,y), font, 1,(255,255,255),2) 18 | cv2.imshow('original', img) 19 | 20 | img = cv2.imread('.../image.jpg') 21 | cv2.imshow('original', img) 22 | 23 | 24 | 25 | cv2.setMouseCallback("original", click_event) 26 | cv2.waitKey(0) 27 | 28 | cv2.destroyAllWindows 29 | -------------------------------------------------------------------------------- /demo2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | ## Written by Andrew Cutts 4 | ### Script to draw a point on each click and connect a line on an (satellite) image using opencv 5 | 6 | import cv2 7 | import numpy as np 8 | 9 | def click_event(event, x, y, flags, param): 10 | if event == cv2.EVENT_LBUTTONDOWN: 11 | ### Place a point on the location of click 12 | cv2.circle(img,(x,y), 3, (0,0,255), -1) 13 | point.append((x,y)) 14 | ### draw a line on the image once we have 2 or more points in point list 15 | if len(point) >= 2: 16 | cv2.line(img,point[-1],point[-2],(0,0,0),2) 17 | cv2.imshow('original', img) 18 | 19 | img = cv2.imread('../image.jpg') 20 | cv2.imshow('original', img) 21 | point = [] 22 | cv2.setMouseCallback("original", click_event) 23 | cv2.waitKey(0) 24 | cv2.destroyAllWindows 25 | -------------------------------------------------------------------------------- /demo3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | ## Written by Andrew Cutts 4 | ### this script will open a new window and display the colour that has been click on in the image 5 | 6 | import cv2 7 | import numpy as np 8 | 9 | def click_event(event, x, y, flags, param): 10 | if event == cv2.EVENT_LBUTTONDOWN: 11 | ## get the colour 12 | red = img[y,x,2] 13 | blue = img[y,x,0] 14 | green = img[y,x,1] 15 | ## plot the point clicked 16 | cv2.circle(img,(x,y), 3, (0,0,255), -1) 17 | ## create a new image called colour_image 18 | colour_image = np.zeros((300,500,3), np.uint8) 19 | ## set the colour 20 | colour_image[:] = [blue,green,red] 21 | ## display the colour image 22 | cv2.imshow('colour', colour_image) 23 | 24 | img = cv2.imread('..//image.jpg') 25 | clone = img.copy() 26 | cv2.imshow('original', img) 27 | 28 | 29 | cv2.setMouseCallback("original", click_event) 30 | cv2.waitKey(0) 31 | 32 | cv2.destroyAllWindows 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # beginners_sat_openCV 2 | Beginners guide to interactive manipulation of satellite data with open cv 3 | 4 | ## Scripts 5 | 6 | [demo1.py](https://github.com/acgeospatial/beginners_sat_openCV/blob/master/demo1.py) - script to display image and right and left click to display information about colours / row & column 7 | 8 | [demo2.py](https://github.com/acgeospatial/beginners_sat_openCV/blob/master/demo2.py) - script to plot a point (in red) on image and draw a line (in black) between points 9 | 10 | [demo3.py](https://github.com/acgeospatial/beginners_sat_openCV/blob/master/demo3.py) - script to display a new window containing the clicked RGB colour using interactive opencv 11 | 12 | Update Jan 2018 -- 13 | I have written up a Guide to using OpenCV and Satellite Iamgery using Juypter Notebooks on my blog http://www.acgeospatial.co.uk/jupyter-notebooks-and-satellite-imagery/ 14 | 15 | Script is here [openCV_simple](https://github.com/acgeospatial/beginners_sat_openCV/blob/master/OpenCV_Display.ipynb) - intro to view sat images in notebooks. Have a go with your own images 16 | 17 | End update --- 18 | 19 | ## Blog 20 | 21 | read below for blog post from 18th June 2017 (using [demo1.py](https://github.com/acgeospatial/beginners_sat_openCV/blob/master/demo1.py)) 22 | 23 | https://www.linkedin.com/pulse/beginners-guide-user-interaction-opencv-python-satellite-andrew-cutts 24 | 25 | ![alt tag](http://www.acgeospatial.co.uk/wp-content/uploads/2017/06/Title.png) 26 | 27 | 28 | # Beginners guide to user Interaction with OpenCV in Python 29 | Posted on 18th June 2017 30 | 31 | I have been working with OpenCV for a while now and I still find the speed of results very impressive. It makes for a compelling case for its use in image processing. Computer Vision, at least to me, represents such an incredible opportunity for Remote Sensing specialists as well as non-specialists. I have been meaning to write a beginners guide for a while now and basing it around user interaction seems to be an excellent introduction to OpenCV. 32 | Installing OpenCV 33 | 34 | You are going to need Python (either Python 3 or Python 2.7) installed on your computer. I generally use Python 2.7 and OpenCV 3.2. Here is a guide to installing OpenCV on windows; have patience it’s worth it! 35 | 36 | http://docs.opencv.org/3.2.0/d5/de5/tutorial_py_setup_in_windows.html 37 | 38 | If you are successful, calling import cv2 from a Python GUI should return no errors. 39 | 40 | Start by opening an image and viewing it 41 | ```python 42 | import cv2 43 | img = cv2.imread('yourimage.jpg') 44 | cv2.imshow('original', img) 45 | cv2.waitKey(0) 46 | cv2.destroyAllWindows 47 | ``` 48 | 49 | This should be enough to view the image… save the file as xxx.py and run it. For this example I am using a clipped Sentinel2 image of Iran. If I have lost you and you don’t know how to run a Python script this is an excellent guide. 50 | 51 | ![alt tag](http://www.acgeospatial.co.uk/wp-content/uploads/2017/06/sentinel2-768x481.jpg) 52 | 53 | By pressing escape you will close all windows, or in this case the image window. 54 | 55 | ## Create an event handler 56 | 57 | Don’t be put off, this is much simpler than you may think. First, build a function to do something when the mouse is clicked. Let’s return the x,y (rows, columns) of the image. 58 | ```python 59 | def click_event(event, x, y, flags, param): 60 | if event == cv2.EVENT_LBUTTONDOWN: 61 | print x, y 62 | ``` 63 | 64 | You have to parse the 5 arguments (event, x, y, flags, param) for opencv to recognise the event (line 1). Line 2 says if the event is a left mouse click then print to the terminal x and y (line 3). Add this to the top of the script (just after the import cv2 line). 65 | 66 | Finally add this line between lines “cv2.imshow(‘original’, img)” and “cv2.waitKey(0)” 67 | ```python 68 | cv2.setMouseCallback("original", click_event) 69 | ``` 70 | 71 | Check the cmd prompt: you should see x,y printed. 72 | 73 | Finally let’s print the RGB values onto the image after a right mouse click 74 | 75 | Add this code to the def click_event 76 | ```python 77 | if event == cv2.EVENT_RBUTTONDOWN: 78 | red = img[y,x,2] 79 | blue = img[y,x,0] 80 | green = img[y,x,1] 81 | print red, green, blue ### prints to command line 82 | strRGB = str(red) + "," + str(green) + "," +str(blue) 83 | font = cv2.FONT_HERSHEY_SIMPLEX 84 | cv2.putText(img,strRGB,(x,y), font, 1,(255,255,255),2) 85 | cv2.imshow('original', img) 86 | ``` 87 | 88 | What is happening here? In lines 2,3&4 we are getting the red, green and blue values from the image. I have ordered them in RGB, but notice the values in the square brackets – the 3rd value is the colour value (OpenCV works in BGR colour space hence the ordering). Line 4 prints the values to the cmd prompt, line 5 creates a string out of these values, eg “255,0,255” – this is assigned to the variable strRGB. Line 6 assigns the font to use and line 7 is where text is assigned. 89 | 90 | I will try and make a bit more sense of line 8: cv2.putText takes 7 arguments above but what do these mean? 91 | 92 | img – this is the image we are working with 93 | 94 | strRGB – this is the text we want to print 95 | 96 | (x,y) – this is the location we are going to put the text 97 | 98 | font – this is the image font; we assigned it in line 6, above 99 | 100 | 1 – this is the scale factor, we are setting it to 1 in this case 101 | 102 | (255,255,255) – this is the colour of the text, in this case white 103 | 104 | 2 – this is the text thickness. 105 | 106 | I hope that is clear. If not, have a look at the official documentation. 107 | 108 | If you run all this code then hopefully when you right click the RGB values will start to appear on the screen. 109 | 110 | ![alt tag](http://www.acgeospatial.co.uk/wp-content/uploads/2017/06/Title.png) 111 | 112 | --------------------------------------------------------------------------------