Skip to content
New issue

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

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

Already on GitHub? # to your account

Added crop functionality #926

Merged
merged 15 commits into from
Aug 17, 2020

Conversation

TheTripleV
Copy link
Contributor

A CropOperation has been added that allows an image to be cropped down to a smaller size.
Users can specify a location and size for the crop. They can also specify the location of the rectangle from which the crop propogates: top left, bottom left, top right, bottom right, center.

Note: I don't know how I would go about writing/testing the code generation.

Example usage:
image

height.intValue()
);

final Mat output = new Mat(input, regionOfInterest).clone();
Copy link
Member

Choose a reason for hiding this comment

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

This can lead to a memory leak. @SamCarlberg Do you know of a good class to use as an example of how this should be done to avoid the memory leak?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think the clone is necessary. The constructor used here uses the same memory as the base image IIRC so there's only minimal overhead. I'll have to take a look when I'm at my computer

Copy link
Member

Choose a reason for hiding this comment

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

Well, we don't want this operation modifying the original input matrix.

@JLLeitschuh
Copy link
Member

JLLeitschuh commented Mar 22, 2019

This is awesome! We'll try to get to it. Can you work on fixing the build so that CI passes?
Looks like you have Checkstyle violations.

@TheTripleV
Copy link
Contributor Author

I fixed the 2 [WhitespaceAfter] errors I believe. However, the [CustomImportOrder] errors are the same ones I see in ResizeOperation (I used ResizeOperation as a starter file).

Also, based on another fork I just saw, I refactored the perform method to not create a new mat every iteration.

@TheTripleV
Copy link
Contributor Author

Right now, I'm just running the Google Checkstyle. Is there a custom checkstyle file for GRIP?

@codecov-io
Copy link

codecov-io commented Mar 23, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@2910a91). Click here to learn what that means.
The diff coverage is 63.82%.

@@            Coverage Diff            @@
##             master     #926   +/-   ##
=========================================
  Coverage          ?   52.93%           
  Complexity        ?        1           
=========================================
  Files             ?      330           
  Lines             ?     8939           
  Branches          ?      562           
=========================================
  Hits              ?     4732           
  Misses            ?     4002           
  Partials          ?      205

@JLLeitschuh
Copy link
Member

Right now, I'm just running the Google Checkstyle. Is there a custom checkstyle file for GRIP?

I haven't actually developed in this codebase in so long that I honestly forget. @SamCarlberg can you help out here?

@SamCarlberg
Copy link
Member

Yes there is: https://github.com/WPIRoboticsProjects/GRIP/blob/master/checkstyle.xml

Run gradlew checkstyleMain to run checkstyle on the production sourcesets, or gradlew check to run all code checks (checkstyle, PMD, ...) as well as unit tests. This is what we have CI use to check pull requests.

@SamCarlberg
Copy link
Member

@TheTripleV With the recent addition of CUDA support, you'll need to change the Mat sockets to use MatWrapper

I can add the code generation in a followup if you're not comfortable with it

@TheTripleV
Copy link
Contributor Author

Cool. I'll take a look at it after my finals next week. I'm just unsure on running tests on GRIP in general. For the operation files, I see test cases/files written. But for code generation, do we just run each piece of generated code manually?

@TheTripleV
Copy link
Contributor Author

I updated the crop operation to use MatWrapper. Interestingly, the operation performs just fine without a clone. This is because every operation that occurs after a crop makes a copy of the data in the MatWrapper.

Should I leave the .clone() out for performace (less memory copies) or should it be in for safety's sake?

@JLLeitschuh
Copy link
Member

@SamCarlberg ^^

@SamCarlberg
Copy link
Member

You can safely omit calling clone()

if (input.isCpu()) {
output.set(input.getCpu().apply(regionOfInterest)); // .clone() optional
} else {
output.set(input.getGpu().apply(regionOfInterest)); // .clone() optional
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the .clone() comments are necessary.

@TheTripleV
Copy link
Contributor Author

I added Java Code Generation. One weird thing I haven't been able to figure out is how it sets default values.

	public void process(Mat source0) {
		// Step Crop0:
		Mat cropInput = source0;
		double cropX = 100;
		double cropY = 100;
		double cropWidth = 50;
		double cropHeight = 50;
		Origin cropOrigin = Center;
		crop(cropInput, cropX, cropY, cropWidth, cropHeight, cropOrigin, cropOutput);

	}

In this step, Origin cropOrigin = Center; should be Origin cropOrigin = Origin.get("Center"); similar to Blur's code generation.

@SamCarlberg
Copy link
Member

There's a hardcoded macro in ui/src/main/resources/edu/wpi/grip/ui/codegeneration/java/macros.vm for doing the enum lookups:

#elseif ($input.type().equals("BlurType"))
		$input.type() ${tMeth.name($input.name())} = ${input.type()}.get("${input.value()}");

You should be able to add an 'or' there to generate the expected enum lookup

#elseif ($input.type().equals("BlurType") || $input.type().equals("Origin"))

@JLLeitschuh
Copy link
Member

Sorry for letting this sit for so long. Happy to merge if it's in a working state.

Copy link
Member

@JLLeitschuh JLLeitschuh left a comment

Choose a reason for hiding this comment

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

LGTM!

@JLLeitschuh
Copy link
Member

Thanks for your contribution @TheTripleV!!

@JLLeitschuh JLLeitschuh merged commit 40cdbc1 into WPIRoboticsProjects:master Aug 17, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants