How to check if an image contains an element I am searchig for

Arak Rachael arakelthedragon at gmail.com
Thu Jun 17 09:35:24 EDT 2021


On Thursday, 17 June 2021 at 09:59:49 UTC+2, Arak Rachael wrote:
> On Thursday, 17 June 2021 at 08:52:55 UTC+2, Peter J. Holzer wrote: 
> > On 2021-06-16 15:51:49 -0700, Arak Rachael wrote: 
> > > On Wednesday, 16 June 2021 at 23:44:02 UTC+2, Chris Angelico wrote: 
> > > > On Thu, Jun 17, 2021 at 7:35 AM Dan Stromberg <drsa... at gmail.com> wrote: 
> > > > > 
> > > > > On Wed, Jun 16, 2021 at 2:04 PM Barry <ba... at barrys-emacs.org> wrote: 
> > > > > 
> > > > > > >>> On Thu, Jun 17, 2021 at 6:06 AM Arak Rachael <arakelt... at gmail.com> 
> > > > > > wrote: 
> > > > > > >>> I have an image from google maps to say and I need to check if it has 
> > > > > > road markings, in order to do that, I believe I need to change the effects 
> > > > > > on the image so the markings and road can be white or something and the 
> > > > > > things I don't need like cars, trees and so on to be black. 
> > [...] 
> > > I understand your concerns. Actually I am doing image processing of 
> > > satellite pictures for smart cars. 
> > Using satellite images probably makes that a lot easier (yes, you wrote 
> > "Google maps", but for some reason I thought of Google Street View). At 
> > least you have a consistent angle (almost from straight above) and know 
> > the scale (given the zoom factor and the latitude). 
> > 
> > hp 
> > 
> > -- 
> > _ | Peter J. Holzer | Story must make more sense than reality. 
> > |_|_) | | 
> > | | | h... at hjp.at | -- Charles Stross, "Creative writing 
> > __/ | http://www.hjp.at/ | challenge!"
> I made the crop code, before I posted the question, I just need the identification part: 
> [code] 
> # Library includes 
> import os 
> import cv2 # image and other special formats processing library 
> # for computer vision 
> import numpy as np 
> from numpy import asarray 
> import PIL 
> from PIL import Image 
> 
> # Global variables 
> # Recommended: move to a separate file 
> 
> # Recommended approach instead of using the "target" 
> # name directly 
> source_directory = "test" # Do not put a slash at the beginning 
> output_directory = "test2" # Do not put a slash at the beginning 
> 
> # Function definitions 
> def image_contains_required_elements(image): # Description: 
> # The function will check if the cropped image contains the 
> # required elements of the road(markings, road and others) 
> 
> # Local variables and initialization 
> raise Exception("Not implemented yet") 
> 
> 
> 
> def split_image(path, dstpath): # Description: 
> # Convert the generated frames(images) from 
> # extract_video(video_path, target_dir_path) to grayscale 
> # and reduce their size to half 
> 
> # Local variables and initialization 
> # Requires the libraries: 
> # import cv2 
> # import os 
> # import pytest 
> # import numpy as np 
> 
> # Processing 
> # Reading an image in default mode 
> #path = r'/home/yordan/devel/python.assignments/topgis-viz.2/data' # Source Folder 
> #dstpath = r'/home/yordan/devel/python.assignments/topgis-viz.2/output_directory' # Destination Folder 
> 
> #path = source_directory # source_directory containing the images before the processing 
> #dstpath = output_directory # output_directory containing the images after the processing 
> """ 
> # Test if target_directory exists 
> try: # Try 
> makedirs(dstpath) # to create target_directory 
> except: # if there is an error 
> print("Directory already exist, images will be written in same folder") # print the error message 
> """ 
> files = os.listdir(path) # Read the files from source_directory and record them in a list 
> 
> for image in files: # For index in list 
> img = cv2.imread(os.path.join(path, image)) # Read the image from path + image name as an array into img 
> 
> # Split image into b, g ,r function 
> gray = img 
> #b, g, r = cv2.split(gray) # Split the image into BGR channels 
> #print(b, g, r) # print the b, g, r channels(codes) 
> crop_img = gray[100:100 + 100, 100:100 + 100] # Crop the image with numpy, x and y are flipped, 
> # example crop_img = img[margin:-margin, margin:-margin] 
> if image_contains_required_elements(crop_img) == True: 
> cv2.imwrite(os.path.join(dstpath, image), crop_img) # Write the image crop_img to a file with name 
> # target_directory + image name 
> 
> # Displaying the image 
> #cv2.imshow("Test", crop_img) # Show the image 
> 
> 
> split_image(source_directory, output_directory) 
> [code]
Thanks for the help guys, I did it with OpenCV and Numpy, here is the code, but please download it, because its in my dropbox unsorted folder and I might delete it in time:
https://www.dropbox.com/s/cr46m538to0j9ja/file_split.py?dl=0

here is a video processing file for who ever needs it:
https://www.dropbox.com/s/39lrxkrfxuws1yd/videotest.py?dl=0


More information about the Python-list mailing list