How to crop an image based on a complex criteria?

2024/10/5 17:21:52

I have a set of similar images like the one below. I want to keep the portion of the image that is within the top red 'irregular' rectangle (green arrows represent the space that I want to keep; anything outside I want to crop out. Is there a python opencv code that would do it for me? I've been trying to figure it out using opencv by playing around with thresholds but it's just not doing it for me.

Original image:

enter image description here The area that I want to keep (the space I want to keep is highlighted by green arrows): enter image description here

Desired output: enter image description here

Thank you so much

Answer

Here's how I would do it. The code that does cv2.imwrite() is just for debug so you can see the various stages and I have put the temporary, intermediate images in where they are produced, but you can just take all the chunks of code and append them together to make one continuous piece of code:

#!/usr/bin/env python3import cv2
import numpy as np# Load image
im = cv2.imread('wavy.png')
copy = im.copy()# Flood fill with white starting from 10,10
cv2.floodFill(copy,mask=None,seedPoint=(10,10),newVal=(255,255,255))
cv2.imwrite('temp1.png',copy)

enter image description here

# Make everything not white into black
copy[~np.all(copy == (255, 255, 255), axis=-1)] = (0,0,0)
cv2.imwrite('temp2.png',copy)

enter image description here

# Make white all the bits we don't want at the bottom of the original image
im[:] |= ~copy# Crop/trim part we want
Ynonzero, Xnonzero, _ = np.nonzero(copy)
res = im[np.min(Ynonzero):np.max(Ynonzero), np.min(Xnonzero):np.max(Xnonzero)]# Save result
cv2.imwrite('result.png',res)

enter image description here

https://en.xdnf.cn/q/119553.html

Related Q&A

Getting current video tag URL with selenium

Im trying to get the current html5 video tag URL using selenium (with python bindings):from selenium import webdriverdriver = webdriver.Chrome() driver.get(https://www.youtube.com/watch?v=9x6YclsLHN0)…

How to determine if two rows are identical (similar) if row 2 contains part of the info from row 1?

Hope you are having a good day. I am currently working with an extremely dirty dataframe containing First Name, Last Name, and Middle Name. One the issues that I am trying to resolve looks like below:F…

Cartopy fancy box

Hello I have been trying to plot data in a Orthographic projection. The data is plotted but I want the box to follow the data limits. Like in this example I am sharing form M_map[enter image descriptio…

discord.py - No DM sent to the user

I am making a discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel. Here is m…

Improve CPU time of conditional statement

I have written an if-elif statement, which I believe not be very efficient:first_number = 1000 second_number = 700 switch = {upperRight: False,upperLeft: False,lowerRight: False,lowerLeft: False,middle…

Why no colon in forming a list from loop in one line in Python?

From this website, there is a way to form a list in Python from loop in one line squares = [i**2 for i in range(10)]My question is, typically, after a loop, there is a colon, e.g., squares = [] for i i…

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…

Python decode unknown character

Im trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors likeascii codec cant decode byte 0xae in position 8: ordinal not…

UnboundLocalError: TRY EXCEPT STATEMENTS

I am currently creating a menu with try except tools. Im trying to create it so if a user enters nothing (presses ENTER) to output:You have not entered anything, please enter a number between 1 and 4Th…

Cant load music into pygame

please help if you can. Cant seem to be able to upload music into my game in progress. It comes up with the error of "cant load"... Would be great if someone got back to me quick, This is a m…