The problem is you cropped the image so that the bounding box coordinates are with respect to the much smaller I1 image, but then when you go to display it, you're displaying it over (the badly named) X (which I assume is the same as the also-badly-named I) rather than I1.
If the apple has irregularities on the edges after segmentation, you can use activecontour() as shown in my attached demo to smooth the outline. Or else you can blur the color channel image before you threshold it.
Best Answer