@@ -84,7 +84,7 @@ void PatchGenerator::generateStream() {
8484 try {
8585 std::tie (level, resampleFactor) = m_inputImagePyramid->getClosestLevelForMagnification (m_magnification, 0.1 );
8686 if (resampleFactor != 1 .0f )
87- reportWarning () << " Requested magnification level does not exist in image pyramid. " <<
87+ reportWarning () << " Requested magnification " << m_magnification << " does not exist in a level of the image pyramid. " <<
8888 " Will now try to sample from a lower level and resize. This may increase runtime." << reportEnd ();
8989 } catch (Exception &e) {
9090 throw Exception (" Unable to generate patches for magnification level " +
@@ -121,13 +121,7 @@ void PatchGenerator::generateStream() {
121121 patchHeight = levelHeight - (patchY * patchHeightWithoutOverlap - overlapInPixelsY)*resampleFactor;
122122 }
123123 int patchOffsetX = (patchX * patchWidthWithoutOverlap - overlapInPixelsX)*resampleFactor;
124- if (patchX == 0 && overlapInPixelsX > 0 ) {
125- patchOffsetX = 0 ;
126- }
127124 int patchOffsetY = (patchY * patchHeightWithoutOverlap - overlapInPixelsY)*resampleFactor;
128- if (patchY == 0 && overlapInPixelsY > 0 ) {
129- patchOffsetY = 0 ;
130- }
131125
132126 if (m_inputMask) {
133127 // If a mask exist, check if this patch should be included or not
@@ -160,10 +154,10 @@ void PatchGenerator::generateStream() {
160154 if (patchWidth < overlapInPixelsX*2 || patchHeight < overlapInPixelsY*2 )
161155 continue ;
162156 auto patch = access->getPatchAsImage (level,
163- patchOffsetX,
164- patchOffsetY,
165- patchWidth,
166- patchHeight);
157+ patchOffsetX < 0 ? 0 : patchOffsetX, // if there is overlap, we will have negative offset at edges
158+ patchOffsetY < 0 ? 0 : patchOffsetY ,
159+ patchWidth + (patchOffsetX < 0 ? patchOffsetX : 0 ), // We have to reduce width and height if negative offset
160+ patchHeight + (patchOffsetY < 0 ? patchOffsetY : 0 ) );
167161
168162 // If patch does not have correct size, pad it
169163 int paddingValue = m_paddingValue;
@@ -174,18 +168,13 @@ void PatchGenerator::generateStream() {
174168 paddingValue = 0 ;
175169 }
176170 }
177- if (patch->getWidth () != (int )(m_width*resampleFactor) || patch->getHeight () != (int )(m_height*resampleFactor)) {
171+ if (patchOffsetX < 0 || patchOffsetY < 0 || patch->getWidth () != (int )(m_width*resampleFactor) || patch->getHeight () != (int )(m_height*resampleFactor)) {
178172 // Edge cases, patches may not be the target patch size. Need to pad.
179- patch = patch->crop (Vector2i (0 , 0 ), Vector2i (m_width*resampleFactor, m_height*resampleFactor), true , paddingValue);
173+ patch = patch->crop (Vector2i (patchOffsetX < 0 ? patchOffsetX : 0 , patchOffsetY < 0 ? patchOffsetY : 0 ), Vector2i (m_width*resampleFactor, m_height*resampleFactor), true , paddingValue);
180174 }
181175 if (resampleFactor > 1 .0f ) {
182176 patch = ImageResizer::create (m_width, m_height, 1 , m_inputImagePyramid->getNrOfChannels () > 1 )->connect (patch)->runAndGetOutputData <Image>();
183177 }
184- if (m_overlapPercent > 0 .0f && (patchX == 0 || patchY == 0 )) {
185- int offsetX = patchX == 0 ? -overlapInPixelsX : 0 ;
186- int offsetY = patchY == 0 ? -overlapInPixelsY : 0 ;
187- patch = patch->crop (Vector2i (offsetX, offsetY), Vector2i (m_width, m_height), true , paddingValue);
188- }
189178
190179 // Store some frame data useful for patch stitching
191180 patch->setFrameData (" original-width" , std::to_string (round (levelWidth/resampleFactor)));
0 commit comments