如何在图像中找到第一个最大的正方形(没有外部库)

2024-04-26 02:29:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有不同的图像,我必须在图像上运行,通过在函数中作为变量给出的颜色,找到图像上第一个最大的正方形。(这意味着,例如,如果存在2个具有相同像素量的正方形,则选择第一个正方形)

这是一个我必须完成的任务,但我被困住了,不知道如何才能达到结果

我们使用的是来自文件immagini的函数load。你知道吗

filename=图像名称

c=RGB代码。你知道吗

我的代码是:

from immagini import *

def quadrato(filename,c):
#Get image from the user (load image)    
img = load(filename)

#print(img[y][x][r,g,b]) 
#Loop trough the image 
#Get the width and height of the image 

counter = []

for y in range(0, len(img[:])):
    for x in range(0, len(img[y][:])):
        #Scan for the R #Scan for the G #Scan for the B
        #If detecs all RGB are the same from the input
        if img[y][x][0] == c[0] and img[y][x][1] == c[1] and img[y][x][2] == c[2]:
            counter.append((x,y))
print (((len(counter)), (x ,y),img[y][x]))

所以我的代码会打印出多少像素的颜色,像素的位置和他的颜色。你知道吗

例如(sqaure(x,y)位置的像素数)

The input ('Img1.png',(255,0,0)) , The output must be (30, (60, 50))

The input ('Img2.png',(0,0,255)) , The output must be (201,(54,240))

谢谢你们,我希望你们能理解我,我很难解释, 如果帖子里有问题,告诉我:)


Tags: andthe代码from图像imageimgfor
1条回答
网友
1楼 · 发布于 2024-04-26 02:29:02

你不应该首先想到单个像素而是水平线。你知道吗

为每个y创建一个连接线列表。如果在搜索的颜色中找到一个像素,并且左侧的像素具有相同的颜色,请将找到的像素添加到最后一行。你知道吗

然后通过y找到相同起点和终点的相邻直线,用它来构建矩形。你知道吗

相关问题 更多 >