Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture analysis using Haralick Descriptors for Computer Vision tasks #8004

Merged
merged 28 commits into from
Sep 5, 2023

Conversation

rzimmerdev
Copy link
Contributor

Describe your change:

I've added a series of methods for calculating the Haralick descriptors, as well as some encapsulating methods for comparing textures between a baseline image and a sequence of comparison images.

  • Add an algorithm?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Nov 27, 2022
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Nov 27, 2022
@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Nov 27, 2022
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



def normalize_image(
image: np.ndarray, cap: float = 255.0, data_type=np.uint8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: data_type

return np.where(image > threshold, 1, 0)


def transform(image: np.ndarray, kind: str, kernel=None) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: kernel

return true_mask, false_mask


def matrix_concurrency(image: np.ndarray, coordinate) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function matrix_concurrency

Please provide type hint for the parameter: coordinate

return matrix / np.sum(matrix)


def haralick_descriptors(matrix: np.ndarray) -> list:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function haralick_descriptors

]


def get_descriptors(masks: tuple[np.ndarray, np.ndarray], coordinate) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function get_descriptors

Please provide type hint for the parameter: coordinate

return np.sqrt(np.sum(np.square(point_1 - point_2)))


def get_distances(descriptors, base) -> list[Any]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function get_distances

Please provide type hint for the parameter: descriptors

Please provide type hint for the parameter: base

return sorted(enumerate(distances), key=lambda tup: tup[1])


def main():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function main

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

return sorted(enumerate(distances), key=lambda tup: tup[1])


def main():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file computer_vision/haralick_descriptors.py, please provide doctest for the function main

@rzimmerdev
Copy link
Contributor Author

Does the main require a doctest as well? gezz...

@algorithms-keeper algorithms-keeper bot removed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Nov 27, 2022
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Nov 27, 2022
@rzimmerdev
Copy link
Contributor Author

@cclauss Awaiting review 😁

@rzimmerdev
Copy link
Contributor Author

@cclauss Any insights yet?

Copy link
Contributor

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay - Pr looks good to me, awesome work!
@cclauss Could you review please

@@ -1,5 +1,6 @@
beautifulsoup4
fake_useragent
imageio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure that imageio/imageio#916 is merged.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how you plan to use ImageIO, I'd recommend using imageio<3.0 or imageio<4.0 instead. We will release v3.0 around the end of January which will remove deprecations. Pinning up to the next version major avoids your code breaking once that happens and puts you in charge of when/how to upgrade.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are a bit different than most repos. In general, we do not pin dependencies because we are not shipping an executable (app or library) and our individual algorithms are almost always self-standing. We hope that tests catch incompatibilities so that they can be fixed as they break. If not then users can report failures so we fix-as-we-go.

rzimmerdev and others added 2 commits December 13, 2022 17:51
No! What if the Kernel is empty?
Example:

>>> kernel = np.zeros((1))
>>> kernel or np.ones((3, 3))
array([[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]])

Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Dec 13, 2022
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 19, 2022
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Dec 20, 2022
@rzimmerdev
Copy link
Contributor Author

Awaiting merge :)

array([[158, 97],
[ 56, 200]], dtype=uint8)
"""
return np.dot(image[:, :, 0:3], [0.299, 0.587, 0.114]).astype(np.uint8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you include a source (e.g., Wikipedia) for those luminance weight values?

Comment on lines +105 to +108
def transform(image: np.ndarray, kind: str, kernel: np.ndarray = None) -> np.ndarray:
"""
Simple image transformation using one of two available filter functions:
Erosion and Dilation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 393 to 406
index = int(input())
q_value = [int(value) for value in input().split()]

# Format is the respective filter to apply,
# can be either 1 for the opening filter or else for the closing
parameters = {"format": int(input()), "threshold": int(input())}

# Number of images to perform methods on
b_number = int(input())

files, descriptors = ([], [])

for _ in range(b_number):
file = input().rstrip()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have prompt messages for each of the input calls

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Sep 5, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Sep 5, 2023
Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still some minor issues, but the major ones have been addressed.

@tianyizheng02 tianyizheng02 merged commit 79b043d into TheAlgorithms:master Sep 5, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Sep 5, 2023
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
…TheAlgorithms#8004)

* Create haralick_descriptors

* Working on creating Unit Testing for Haralick Descriptors module

* Type hinting for Haralick descriptors

* Fixed docstrings, unit testing and formatting choices

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixed line size formatting

* Added final doctests

* Changed main callable

* Updated requirements.txt

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update computer_vision/haralick_descriptors.py

No! What if the Kernel is empty?
Example:

>>> kernel = np.zeros((1))
>>> kernel or np.ones((3, 3))
array([[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]])

Co-authored-by: Christian Clauss <cclauss@me.com>

* Undone wrong commit

* Update haralick_descriptors.py

* Apply suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix ruff errors in haralick_descriptors.py

* Add type hint to haralick_descriptors.py to fix ruff error

* Update haralick_descriptors.py

* Update haralick_descriptors.py

* Update haralick_descriptors.py

* Update haralick_descriptors.py

* Try to fix mypy errors in haralick_descriptors.py

* Update haralick_descriptors.py

* Fix type hint in haralick_descriptors.py

---------

Co-authored-by: Rafael Zimmer <rzimmerdev@monmo.localdomain>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants