Automated image processing algorithms can improve the quality, efficiency, and consistency of classifying the morphology of heterogeneous carbonate rock and can deal with a massive amount of data and images seamlessly. Geoscientists and petroleum engineers face difficulties in setting the direction of the optimum method for determining petrophysical properties from core plug images of optical thin-sections, Micro-Computed Tomography (μCT), or Magnetic Resonance Imaging (MRI). Most of the successful work is from the homogeneous and clastic rocks focusing on 2D images with less focus on 3D and requiring numerical simulation. Currently, image analysis methods converge to three approaches: image processing, artificial intelligence, and combined image processing with artificial intelligence. In this work, we propose two methods to determine the porosity from 3D μCT and MRI images: an image processing method with Image Resolution Optimized Gaussian Algorithm (IROGA); advanced image recognition method enabled by Machine Learning Difference of Gaussian Random Forest (MLDGRF). Meanwhile, we have built reference 3D micro models and collected images for calibration of the IROGA and MLDGRF methods. To evaluate the predictive capability of these calibrated approaches, we ran them on 3D μCT and MRI images of natural heterogeneous carbonate rock. We also measured the porosity and lithology of the carbonate rock using three and two industry-standard ways, respectively, as reference values. Notably, IROGA and MLDGRF have produced porosity results with an accuracy of 96.2% and 97.1% on the training set and 91.7% and 94.4% on blind test validation, respectively, in comparison with the three experimental measurements. We measured limestone and pyrite reference values using two methods, X-ray powder diffraction, and grain density measurements. MLDGRF has produced lithology (limestone and pyrite) volume fractions with an accuracy of 97.7% in comparison to reference measurements.