简体   繁体   English

图像处理概述

[英]Image Processing Outlines

I'm writing a program that does basic image processing. 我正在编写一个进行基本图像处理的程序。

Keep in mind that the images are in grayscale, not RGB, also, I'm fairly new to Python, so an explanation of what I'm doing wrong/right would be incredibly helpful. 请记住,图像是灰度的,而不是RGB,而且我对Python还是很陌生,所以对我做错了/正确的事情的解释会非常有用。

I'm trying to write an outline algorithm that follows this set of rules: 我正在尝试编写遵循以下规则的大纲算法:

All light pixels in the original must be white in the outline image. 轮廓图像中原稿的所有浅色像素必须为白色。 All dark pixels on the edges of the image must be black in the outline image. 轮廓图像中图像边缘上的所有暗像素必须为黑色。 If a pixel that is not on an edge of the image is dark and all of the 8 surrounding pixels are dark, this pixel is on the inside of a shape and must be white in the outline image. 如果不在图像边缘上的像素是暗的,而周围的所有8个像素都是暗的,则此像素在形状的内部,并且在轮廓图像中必须为白色。 All other dark pixels must be black in the outline image. 轮廓图像中所有其他深色像素必须为黑色。

So far I have this: 到目前为止,我有这个:

def outlines(image):
    """
    Finds the outlines of shapes in an image.  The parameter must be
    a two-dimensional list of pixels.  The return value is another
    two-dimensional list of pixels which describes an image showing
    outlines of the shapes in the original image.  Each pixel in the
    return value will be either black (0) or white (255).
    """
    height=len(image)
    width=len(image[0])
    new_image=[]
    for r in range(height):
        new_row=[]
        index=0
        for c in range(width):
            if image[r][c]>128:
                new_row.append(255)
            if image[r][c]<=128:
                new_row.append(0])
        new_image.append(new_row)

Can someone show me how to implement the algorithm into my outlines function? 有人可以告诉我如何将算法实现到我的轮廓函数中吗?

Thanks in advance. 提前致谢。 Edit: This is an assignment for my University Comp Sci class, I'm not asking for someone to do my homework, rather because I've virtually no idea what the next step is. 编辑:这是我的大学计算机科学课的作业,我不是要别人做作业,而是因为我实际上不知道下一步是什么。 Edit2: If someone could explain to me a simple edge detection function that is similar to the algorithm I need to create I would appreciate it. Edit2:如果有人可以向我解释一个简单的边缘检测功能,该功能类似于我需要创建的算法,我将不胜感激。

In addition to check if your pixel is dark or clear, you should also check, when dark, if the rest of pixels around are also dark in order to make that point white instead. 除了检查像素是否暗或清晰外,还应检查在黑暗时像素周围的其余像素是否也暗,以使该点变为白色。

Check this function and try to use it for that purpose: 检查此功能并尝试将其用于该目的:

def all_are_dark_around(image, r, c):
    # range gives the bounds [-1, 0, 1]
    # you could use the list directly. Probably better for this especific case
    for i in range(-1,2):              
        for j in range(-1,2):
            # if the pixel is clear return False.
            # note that image[r+0][c+0] is always dark by definition
            if image[r+i][c+j] <= 128:  
                return False
    #the loop finished -> all pixels in the 3x3 square were dark
    return True

Advices: 建议:

  1. note that you should never check image[r][c] when r or c are equal to 0 or to height or width . 请注意,当rc等于0heightwidth时,切勿检查image[r][c] That is, when the checked pixel is in the border because in this case there is at least one side where there is no adjacent pixel to look at in the image and you will get an IndexError 也就是说,当选中的像素位于边框中时,因为在这种情况下,图像中至少有一侧没有相邻像素要看,因此您将得到IndexError
  2. don't expect this code to work directly and to be the best code on terms of efficiency or good style. 不要指望这种代码可以直接工作,并且就效率或良好样式而言,它们是最好的代码。 This is a hint for your homework. 这是作业的提示。 You should do it. 你应该去做 So look at the code, take your time ( optimally it should be equal or longer than the time it took to me to write the function ), understand how it works and adapt it to your code fixing any exceptions and border situations you encounter in the way. 因此,请看一下代码,花点时间( 最好是等于或长于我编写该函数的时间 ),了解它的工作原理并将其适应您的代码,以解决您在代码中遇到的任何异常和边界情况方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM