dimanche 24 mai 2020

Python: Implement a function for find a pattern in an image

I need help with an exercise in python and I have not been able to solve it. The exercise reads like this:

Find a pattern in an image.

You are given 2 images: a base image and a pattern which may appear somewhere on this image. Each image is represented by a list of strings, where each element of the list represents a row of pixels of the image, and each character represents one pixel. Understanding this encoding is not necessary to solve this problem, but for your information, a detailed explanation is provided below

You must return the position x, y of this pattern in the image, or (-1, -1) if the patter is not found. If the pattern appears multiple times, return the position of the highest one (the smallest y), and in case of equality, the leftmost one (the smallest x).

The position of the pattern is represented by the coordinates x, y of it is top-left corner. X represents the column, y represents the row, and the coordinates (0, 0) represents the top-left corner. Implementation.

Implement the function, where the parameters are:

  • imageWidth: the image width
  • imageHeight: the image height
  • image: the image as a list of strings, where each character represents a pixel
  • patternWidth: the pattern width
  • patternHeight: the pattern height
  • pattern: the pattern as a list of strigns, where each character represents a pixel

and which should return:

  • if the pattern is present in the image: the position x, y, as a list of 2 integers, representing the top-left corner of the first pattern, going from top to bottom and left to right.
  • If the pattern is not present in the image: (-1, -1)

Victory Conditions

  • The pattern is indeed located at coordinates x, y.
  • If the pattern appears multiple times, return the position of the highest one (the smallest y), and in case of equality, the leftmost one (the smallest x).

Defeat Conditions

  • The inner image that starst at coordinates x, y does not match the pattern.
  • You take longer than 1 second to answer.
  • There is another matching pattern above or on the left.

The code supplied for the exercise is as follows:

import sys
import math
from contextlib import redirect_stdout


def solve(image_width, image_height, image, pattern_width, pattern_height, pattern):
    # Write your code here
    # To debug: print("Debug messages...", file=sys.stderr)
    return []

# Ignore and do not change the code below
def main():
    image_width, image_height = [int(i) for i in input().split()]
    image = []
    for i in range(image_height):
        image.append(input())
    pattern_width, pattern_height = [int(i) for i in input().split()]
    pattern = []
    for i in range(pattern_height):
        pattern.append(input())
    with redirect_stdout(sys.stderr):
        coordinates = solve(image_width, image_height, image, pattern_width, pattern_height, pattern)
    for i in range(2):
        print(coordinates[i])

if __name__ == "__main__":
    main()

Aucun commentaire:

Enregistrer un commentaire